Skip to main content

50. Merge Intervals

mediumAsked at Plaid

Merge overlapping intervals. Plaid asks this because consolidating overlapping batch-windows (when ETL retries cover overlapping time ranges) is exactly this primitive.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Plaid loops.

  • Glassdoor (2025)Plaid backend onsite — ETL window merge.
  • Blind (2026)Plaid SWE II OA.

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]]

Example 2

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

Approaches

1. All-pairs overlap check

For each pair (i, j), check if they overlap; merge in place. Repeat until stable.

Time
O(n^3)
Space
O(n)
// Pairwise. Don't ship.

Tradeoff: Cubic. TLE.

2. Sort by start, then sweep

Sort intervals by start. Sweep left to right; merge into the last accepted interval if they overlap; otherwise push as new.

Time
O(n log n)
Space
O(n) for sort/output
function merge(intervals) {
  intervals.sort((a, b) => a[0] - b[0]);
  const out = [intervals[0]];
  for (let i = 1; i < intervals.length; i++) {
    const last = out[out.length - 1];
    if (intervals[i][0] <= last[1]) {
      last[1] = Math.max(last[1], intervals[i][1]);
    } else {
      out.push(intervals[i]);
    }
  }
  return out;
}

Tradeoff: O(n log n) sort, O(n) sweep. The 'extend last accepted interval' pattern is the canonical sweep-line.

Plaid-specific tips

Plaid grades this on the sort-then-sweep pattern. Bonus signal: mention that after sorting, overlap = next.start <= last.end (with <= because touching intervals also merge per the spec). Connect to ETL retry-window merging where two overlapping retries should produce one combined fetch.

Common mistakes

  • Using < instead of <= when checking overlap — misses touching intervals.
  • Mutating the input array's intervals — fine but document it.
  • Forgetting to update last.end with max(last.end, current.end) — using just current.end can shrink the window.

Follow-up questions

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

  • Insert interval (LC 57) — add one interval to a sorted list.
  • Interval list intersections (LC 986).
  • Stream version with bounded memory.

Solve it now

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

Output

Press Run or Cmd+Enter to execute

FAQ

Why sort by start, not end?

Sort-by-start lets the sweep extend the rightmost in-progress interval. Sort-by-end works for a different problem (max non-overlapping intervals).

What about touching intervals like [1,3] and [3,5]?

The spec says 'overlapping' which traditionally includes touching. Use <= in the check. Verify with the interviewer.

Practice these live with InterviewChamp.AI

Drill Merge Intervals and other Plaid interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →