v9.1.1
Menu

Merge Intervals

mediumPROSorting & SearchingNetflix 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 <= len(intervals) <= 10^4
  • 0 <= start <= end <= 10^4

More from Netflix DSA

PRO

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test results

Sign in to run your code against the test cases.