See my jobs
Home/Problems/Merge intervals

Merge Intervals

Updated Nov 17, 2026

18 of 909 coded candidate reports (Jan 2025 to Nov 2026) mention Merge Intervals or interval union; 15 are Meta; none in the last 90 days

Given a set of ranges, combine the ones that overlap. The base problem is a sort and one pass. Interviews spend more time on the variants: Meta reports keep asking for the union of two already-sorted lists, and recent Google and LinkedIn reports push it to K lists, streams and AI-assisted increments.

In short
  • 18 of 909 reports mention it: Meta 15, Google 2, plus one with no company named. None are from the last 90 days.
  • At least 7 of Meta's 15 describe the two-sorted-lists version. Practise that one as much as the base problem.
  • The base solution sorts by start, sweeps once, and either extends the last merged range or starts a new one. It runs in O(n log n) time.
  • Ask whether touching ranges like [3,5] and [5,7] merge. It changes one character in the code.
  • Follow-ups: two lists in O(n + m), K lists with a heap, intervals arriving one at a time, and counting maximum overlap.

The problem

You get a list of ranges, each written as [start, end] with start <= end. The list is in no particular order. Combine every group of ranges that overlap into a single range that covers them all, and return the resulting list of non-overlapping ranges sorted by start.

Before coding, ask whether ranges that only touch (one ends exactly where the next begins) count as overlapping. The usual answer is yes, but confirm.

Examples

InputOutputWhat it tests
[[2,5], [4,9], [12,14], [20,22]][[2,9], [12,14], [20,22]]Basic overlap
[[6,8], [1,3]][[1,3], [6,8]]Input is not sorted
[[1,10], [3,4], [5,6]][[1,10]]One range contains the others
[[3,5], [5,7]][[3,7]]Touching ranges (if touching counts)
[][]Empty input

Who asks it

We coded 909 first-hand candidate reports that list questions, dated January 2025 to November 2026. 18 mention Merge Intervals or a union of interval lists. Counting only single-company reports: Meta 15, Google 2. None of the 18 are from the last 90 days. Most of Meta's reports in our data come from LeetCode Discuss in early 2025, which is why Meta problems rank high overall but rarely appear recently.

The broader theme of interval and sweep-line problems is more current: 8 reports, 4 of them from the last 90 days, mostly Google (5) and Meta (2).

CompanyWhat candidates describe
MetaAt least 7 of the 15 describe merging two sorted interval lists or their union, e.g. E4 phone screen, Jan 2025 [4]; E4 infra onsite, Feb 2025 [6]; E4, Sep 2025 [7]. An E6 phone screen asked a union variant of interval list intersection [5].
GoogleSenior onsite, Aug 2026: union of sorted interval lists, then K lists, then a streaming version; the candidate said follow-ups decided the round [1]. L4, Feb 2025: merging interval lists with extra restrictions [8].
LinkedInStaff AI-enabled coding round, Jan 2026: a modified merge intervals problem in three increments. The candidate reported that prompts and verification were evaluated, and that the optimal solution carried weight [3]. (Not counted in the 18, which come from a different matching pass.)
No company namedMid-level candidate, Jun 2026: merge intervals and line sweep problems came up in take-home assignments [2].

Approach: brute force to optimal

ApproachTimeSpaceNotes
Repeatedly scan all pairs, merge any overlapping pair, restartO(n^2) per pass, up to O(n^3)O(n)Correct but slow; fine as a first sentence, not as code
Sort by start, then one sweepO(n log n)O(n) for output (sort may use more)The standard answer
Input already sorted by startO(n)O(n)Just the sweep
Two sorted, non-overlapping listsO(n + m)O(n + m)Two pointers, merge-sort style, then the same sweep step
K sorted listsO(N log K)O(K) heap + outputMin-heap of the next interval from each list

Sorting works because once ranges are ordered by start, any range that overlaps the current merged block must come right after it. If the next range starts after the block ends, nothing later can overlap the block either, because later ranges start even further right. So one pass suffices.

In the sweep step, look at the last range in the output. If the new range starts at or before its end, extend the end to the larger of the two ends. Otherwise append the new range. The max matters: a range can be fully inside the block, as in [[1,10],[3,4]].

Python solution

def merge(intervals: list[list[int]]) -> list[list[int]]:
    """Sort by start, then sweep once. O(n log n) time."""
    if not intervals:
        return []
    intervals = sorted(intervals, key=lambda iv: iv[0])
    merged = [list(intervals[0])]
    for start, end in intervals[1:]:
        last = merged[-1]
        if start <= last[1]:           # touches or overlaps
            last[1] = max(last[1], end)
        else:
            merged.append([start, end])
    return merged


def merge_two_sorted(a: list[list[int]], b: list[list[int]]) -> list[list[int]]:
    """Follow-up: a and b are each sorted and non-overlapping. O(n + m)."""
    i = j = 0
    out = []
    while i < len(a) or j < len(b):
        if j == len(b) or (i < len(a) and a[i][0] <= b[j][0]):
            start, end = a[i]
            i += 1
        else:
            start, end = b[j]
            j += 1
        if out and start <= out[-1][1]:
            out[-1][1] = max(out[-1][1], end)
        else:
            out.append([start, end])
    return out

merge does not modify the caller's list: sorted makes a copy and new lists are built for the output. Mention that if you sort in place instead, since some interviewers ask.

If touching ranges should stay separate, change <= to < in both functions.

Follow-up questions

  • Two sorted lists (Meta [4][6][7]): do not concatenate and re-sort. Walk both with two pointers, always taking the range with the smaller start, and apply the sweep step. O(n + m). See merge_two_sorted above.
  • Intersection of two sorted lists (Meta [5]): two pointers again. The overlap of two ranges is [max of starts, min of ends] if that is non-empty. Advance whichever range ends first.
  • K sorted lists (Google [1]): put the first range of each list in a min-heap keyed by start. Pop the smallest, apply the sweep step, push the next range from the same list. O(N log K).
  • Ranges arrive one at a time (Google [1]): keep merged ranges in a sorted structure keyed by start (for example sortedcontainers.SortedList, or a balanced tree in Java or C++). On insert, find neighbors that overlap, remove them, insert the combined range. Each range is inserted and removed at most once, so the cost is O(log n) amortized.
  • Insert one range into an already merged, sorted list: one pass. Copy ranges that end before it, absorb ranges that overlap it, copy the rest. O(n).
  • Maximum number of overlapping ranges (the meeting rooms family): turn each range into a +1 event at start and a -1 event at end, sort events, and track the running sum. Decide whether an end and a start at the same time overlap, and order events to match.
  • Input too large for memory: external sort by start, then stream the sweep. The sweep only needs the current block.

The AI-allowed version

The one AI-round report we have for this problem is LinkedIn's: a Staff candidate in January 2026 got a modified merge intervals task in three increments. They reported that prompts and verification were evaluated, not only the final code, and that reaching the optimal solution still mattered [3]. A separate LinkedIn senior report describes the same 'coding with AI' setup in CoderPad: boilerplate, tests, an AI-assist tab and three parts [10]. Meta's careers FAQ says many of its interviews now include a built-in AI assistant [9].

An AI tool will write the base merge instantly, so the score comes from the increments.

  • You still settle touching vs overlapping and closed vs half-open ranges with the interviewer, explain why sorting by start is enough, and choose the right structure for each increment (two pointers, heap, sorted container) along with its complexity.
  • In generated code, look for sorting by end instead of start, and for overwriting the end instead of taking the max, which fails the containment case. Check < vs <= on touching ranges and whether the code mutates the input list. On the two-list follow-up, concatenate-and-sort works but misses the O(n + m) point.
  • To verify fast, turn the edge cases from the examples table into asserts (empty, unsorted, containment, touching, single range). For a stronger check on small integer ranges, mark every covered point in a set, rebuild the ranges from the set, and compare with the function's output on a few hundred random inputs.
  • Say what you are checking. In the LinkedIn report, verification itself was graded [3].

Common mistakes

  • Forgetting to sort, or sorting by end.
  • Setting the merged end to the new range's end instead of the max of both ends.
  • Not asking whether touching ranges merge.
  • Comparing the new range with the previous input range instead of the last merged range.
  • For two sorted lists, concatenating and sorting again without mentioning the linear approach.
  • Changing the caller's list in place without saying so.

How to prepare

Base plus two-list version, cold

Write both functions above from memory in under 15 minutes total. The two-list version is the one Meta reports keep naming.

Practise the escalation

Two lists, then K lists with a heap, then a stream. That is the order the Google senior report describes [1].

Learn the sweep-line pattern

Maximum overlap and meeting rooms use the same event-sorting idea. The interval theme is more current in our data than this single problem.

Practise Merge Intervals with follow-ups

A mock coding interview that moves from the base problem to two lists, K lists and a stream.

Start free mock

FAQ

Do touching intervals merge?

Usually yes, but ask. In code it is the difference between <= and < in the overlap check.

Why sort by start and not by end?

Sorting by start guarantees that anything overlapping the current block comes right after it, so one pass works. Sorting by end breaks this: a long range that starts early can appear after short ranges it covers.

Is Merge Intervals still asked in 2026?

None of the 18 reports that name it are from the last 90 days. But interval problems as a theme have 4 recent reports, and 2026 reports from Google and LinkedIn use extended versions [1][3]. Prepare the variants, not only the base problem.

Which companies ask it?

Mostly Meta (15 of 18 in our data) and Google (2). LinkedIn used a modified version in an AI-enabled round [3].

What is the time complexity?

O(n log n) for the sort plus O(n) for the sweep. If the input is already sorted, O(n). Two sorted lists: O(n + m). K lists: O(N log K).

Sources

  1. Candidate report · PracHub: Google senior SWE, five onsite rounds where follow-ups decide (union of sorted interval lists) · Aug 2026
  2. Candidate report · Reddit r/cscareerquestions: 2 YOE mid-level job search experience · Jun 25, 2026
  3. Candidate report · LeetCode Discuss: LinkedIn Staff AI-enabled coding round, modified merge intervals in 3 increments · Jan 9, 2026
  4. Candidate report · LeetCode Discuss: Meta E4 screen pass (merge two interval lists) · Jan 2025
  5. Candidate report · LeetCode Discuss: Meta E6 phone screen pass (interval list intersection, union variant) · Jan 2025
  6. Candidate report · LeetCode Discuss: Meta E4 infra onsite (merge intervals from two lists) · Feb 2025
  7. Candidate report · LeetCode Discuss: Meta E4 (merge intervals union) · Sep 2025
  8. Candidate report · LeetCode Discuss: Google L4 (merge interval lists with restrictions) · Feb 2025
  9. Official · Meta Careers: hiring process FAQ (built-in AI assistant)
  10. Candidate report · LeetCode Discuss: LinkedIn senior SWE offer, 'Coding with AI' on CoderPad · Aug 22, 2026

We quote briefly, never name posters, and correct errors on request: report an error.

Related