Skip to content
Decoder

Merge Intervals

Medium

Problem

Given a list of intervals [start, end], merge every group that overlaps and return the result sorted by start. Intervals that touch (one ends where the next starts) count as overlapping.

Example 1

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

Example 2

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

Example 3

Input
[[4, 7], [1, 4]]
Output
[[1, 7]]
Why
input may be unsorted

Constraints

  • 0 <= intervals.length <= 10^4
  • 0 <= start <= end <= 10^4

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.