Skip to main content

50. Merge Intervals

mediumAsked at Vercel

Merge overlapping intervals. Vercel asks this for the sort-then-sweep pattern — the same shape as their cache-invalidation merging (overlapping TTL windows collapse into a single invalidation pass).

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

Source citations

Public interview reports confirming this problem appears in Vercel loops.

  • Glassdoor (2025-Q4)Vercel platform onsite; sort + sweep expected.
  • Blind (2026-Q1)Listed in Vercel routing engineer screen.

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

Explanation: Intervals touching at a point are considered overlapping.

Approaches

1. All-pairs union

For each interval, find all overlapping; merge.

Time
O(n^2)
Space
O(n)
// O(n^2). Mention only to set the bar.

Tradeoff: Quadratic.

2. Sort by start, sweep and merge (optimal)

Sort intervals by start. Iterate; if current overlaps with last merged (current.start <= last.end), extend last.end. Else append current.

Time
O(n log n)
Space
O(n) 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];
    const cur = intervals[i];
    if (cur[0] <= last[1]) {
      last[1] = Math.max(last[1], cur[1]);
    } else {
      out.push(cur);
    }
  }
  return out;
}

Tradeoff: O(n log n) dominated by sort. The sweep is O(n). The `Math.max(last[1], cur[1])` handles the case where cur is fully inside last.

Vercel-specific tips

Vercel grades the sort + sweep. Bonus signal: explaining that touching intervals (LC's example [4,5] after [1,4]) ARE considered overlapping under the `<=` condition. They may follow up with 'now insert one new interval into a sorted list' (LC 57) — binary search is the optimal there.

Common mistakes

  • Using `<` instead of `<=` — fails on touching intervals.
  • Forgetting Math.max on the merge — extends to cur[1] even when last[1] is larger.
  • Sorting by end instead of start — works but the logic gets weirder.

Follow-up questions

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

  • Insert Interval (LC 57) — one interval into a sorted list.
  • Meeting Rooms II (LC 253) — count overlapping intervals at any point.
  • Employee Free Time (LC 759) — interval complement.

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?

Sorting by start lets us sweep left-to-right and only check overlap with the immediately previous merged interval. Any earlier interval can't reach cur if it didn't reach the last merged.

Are touching intervals overlapping?

Per LeetCode example 2, yes — [1,4] and [4,5] merge into [1,5]. Use `<=` not `<`. Real-world systems may differ; ask the interviewer.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →