IRInterview Ready
← All patterns

Merge Intervals

Sort intervals by start time, then sweep once, merging overlapping ranges as you go.

When to use it

  • Any problem involving overlapping ranges: meeting rooms, calendar conflicts, interval merging/insertion.

Signals in the problem statement

  • Input is a list of [start, end] pairs.
  • Keywords: overlap, merge, conflict, free time.

Common pitfalls

  • Forgetting to sort first — the sweep only works correctly on sorted input.
  • Using `<` instead of `<=` for adjacency (deciding whether touching intervals should merge).

Practice Problems

Merge Intervals

Medium

Asked at: Google, Facebook, Amazon

Given a collection of intervals, merge all overlapping intervals.

Time: O(n log n) · Space: O(n)