interviewmind

Practice room

Call time 00:00Get the app
D
Dana · Interviewer
You
You
Captions

Press Play and Dana asks the question out loud.

Questions1/17

CodingMedium

Merge Intervals

You’re given an array intervals, where intervals[i] = [start, end]. Merge every interval that overlaps another, and return the intervals that remain, sorted by start.

Two intervals overlap when one starts at or before the moment the other ends, so [1, 4] and [4, 5] merge into [1, 5].

Example 1

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

[1,3] and [2,6] overlap, so they merge into [1,6].

Example 2

Inputintervals = [[1,4],[4,5]]Output[[1,5]]

Touching ends count as an overlap.

Constraints

  • 1 ≤ intervals.length ≤ 10⁴
  • intervals[i].length = 2
  • 0 ≤ start ≤ end ≤ 10⁴

Tab indents. Press Escape, then Tab, to move focus out of the editor.

TypeScriptLn 1, Col 1
Practice room — InterviewMind