Skip to main content

56. Merge Intervals

mediumAsked at Netflix

Short answer

  • Best approach: Sort by start, then one-pass sweep. Sort by start. Iterate; if the current interval's start <= last merged interval's end, extend the last; otherwise append a new interval.
  • Time O(n log n), space O(n) for output (O(1) extra if mutating in place)
  • Verified

Given an array of intervals, merge all overlapping intervals and return the result. Netflix asks this as the canonical 'sort then sweep' problem — they want you to articulate why sorting by start unlocks the linear scan.

By Sam K., Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Netflix loops.

  • Glassdoor (2026-Q1)— Netflix L4/L5 onsite reports list Merge Intervals as their highest-frequency array problem in 2026-Q1.
  • Blind (2025-12)— Netflix SWE writeups consistently mention Merge Intervals as their go-to interval problem.

Problem

Given an array of intervals where intervals[i] = [start_i, end_i], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.

Constraints

  • 1 <= intervals.length <= 10^4
  • intervals[i].length == 2
  • 0 <= start_i <= end_i <= 10^4

Examples

Example 1

Input
intervals = [[1,3],[2,6],[8,10],[15,18]]
Output
[[1,6],[8,10],[15,18]]

Explanation: Intervals [1,3] and [2,6] overlap, merge into [1,6].

Example 2

Input
intervals = [[1,4],[4,5]]
Output
[[1,5]]

Explanation: [1,4] and [4,5] are considered overlapping.

Approaches

1. Brute-force pairwise merge until stable

Repeatedly scan all pairs; merge any overlapping pair; restart. Stop when no merges happen in a full pass.

Time
O(n^3) worst case
Space
O(n)
function mergeBrute(intervals) {
  let out = intervals.slice();
  let changed = true;
  while (changed) {
    changed = false;
    outer: for (let i = 0; i < out.length; i++) {
      for (let j = i + 1; j < out.length; j++) {
        if (out[i][0] <= out[j][1] && out[j][0] <= out[i][1]) {
          out[i] = [Math.min(out[i][0], out[j][0]), Math.max(out[i][1], out[j][1])];
          out.splice(j, 1);
          changed = true;
          break outer;
        }
      }
    }
  }
  return out;
}

Tradeoff: Correct but cubic. Mention it only to motivate the sort + sweep insight.

2. Sort by start, then one-pass sweep (optimal)

Sort by start. Iterate; if the current interval's start <= last merged interval's end, extend the last; otherwise append a new interval.

Time
O(n log n)
Space
O(n) for output (O(1) extra if mutating in place)
function merge(intervals) {
  intervals.sort((a, b) => a[0] - b[0]);
  const out = [];
  for (const [s, e] of intervals) {
    if (out.length === 0 || s > out[out.length - 1][1]) {
      out.push([s, e]);
    } else {
      out[out.length - 1][1] = Math.max(out[out.length - 1][1], e);
    }
  }
  return out;
}

Tradeoff: O(n log n) dominated by sort. The sweep is O(n). After sorting by start, two intervals overlap iff the current start <= previous end.

Netflix-specific tips

Netflix interviewers want you to explicitly justify the sort: 'Sorting by start guarantees that overlaps can only happen with the most recent merged interval — no need to look further back.' That single sentence carries the entire algorithm. They'll also probe edge cases: empty input, single interval, intervals that just touch ([1,4] and [4,5]).

Common mistakes

  • Using `s >= out[out.length - 1][1]` instead of `s > ...` — incorrectly leaves [1,4] and [4,5] as two separate intervals.
  • Sorting by end instead of start — works for some interval problems but not this one (overlapping starts can be in any order).
  • Forgetting that the output's last end must be the MAX of the two ends, not just the current one.

Follow-up questions

An interviewer at Netflix may pivot to one of these next:

  • Insert Interval (LC 57) — same problem but the new interval is given separately and you can leverage the pre-sorted input.
  • Meeting Rooms II (LC 253) — count the minimum number of rooms needed.
  • Employee Free Time (LC 759) — merge across multiple employees' schedules.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Ctrl+Enter to execute

FAQ

Why sort by start and not by end?

Because after sorting by start, the next interval can only overlap with the most recent merged interval — if it doesn't overlap, no earlier interval can either (their ends are all <= the recent end). Sorting by end loses that locality.

Are [1,4] and [4,5] considered overlapping?

Per the LC problem definition, yes — they touch at point 4. The merge condition is start <= prev_end, using a non-strict inequality.

Free learning resources

Curated free links for this problem.

Originally published on InterviewChamp.AI

Companies that also ask Merge Intervals