Merge Intervals

Merge overlapping intervals into a condensed list

MediumIntervalsSorting

Merge all overlapping intervals and return the condensed list.

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

Constraints

  • 1 ≤ intervals.length ≤ 10⁴
  • -10⁵ ≤ startᵢ ≤ endᵢ ≤ 10⁵

Approach

  1. Sort intervals by starting coordinate.
  2. Iterate and merge when the current start is ≤ previous end.

Hints

Show hint 1

Sorting by the first element lets you scan once while merging.

Show hint 2

Keep a result list and compare against the last merged interval.

Follow-up questions

What an interviewer might ask once you have a working solution.

  • How would you merge in-place with O(1) extra space?

Practice this with an AI interviewer

Explain your approach out loud, write Python or JavaScript, run it against hidden tests (including large inputs), and get a scored debrief.

Start this problem