Skip to interview prep
Apexed

39. Merge Intervals

MediumIntervals

Given intervals where each is [start, end], merge all overlapping intervals (touching ends count as overlapping) and return the result sorted by start.

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⁴

Tab indents · Ctrl/⌘ + Enter runs · Esc then Tab leaves the editor

Run checks these cases; the expected output comes from the reference solution. Submit judges all 5 hidden and sample tests.

Code runs only in your browser in an isolated sandbox. Progress and submissions are saved on this device.

Merge Intervals — coding interview practice — Apexed