v9.1.1
Menu

Merge Intervals

mediumFREEIntervals & SchedulingUber DSA

Given an array of intervals [start, end], merge all overlapping intervals and return the result.

Input: intervals as start:end comma-separated. Output: merged intervals, one per line.

Example

Input
1:3,2:6,8:10,15:18
Output
1 6
8 10
15 18
Reasoning
  • The input intervals are first split into individual intervals: [1:3], [2:6], [8:10], [15:18]
  • Overlapping intervals are merged: [1:3] and [2:6] overlap, resulting in [1:6], while [8:10] and [15:18] do not overlap with any other intervals
  • The merged intervals are then checked for any further overlaps, but since [1:6], [8:10], and [15:18] do not overlap, they remain as separate intervals
  • The final merged intervals are output in the required format: 1 6 8 10 15 18

Constraints

  • 1 <= intervals.length <= 10^4
  • intervals[i].length == 2

More from Uber DSA

solution.py

Test results

Sign in to run your code against the test cases.