Back to Practice
#0255
Merge Overlapping Intervals
MediumDSA28 min20 XP
Problem
Given intervals [start, end], merge all overlapping intervals. Treat [1, 4] and [4, 5] as mergeable because they touch.
Why This Matters
Merging intervals is the base skill behind calendars, timeline cleanup, booking systems, and sweep-line problems.
Function Signature
def merge_intervals(intervals):
Examples
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 become [1,6].
Constraints
- Return the exact requested value.
- Handle edge cases cleanly.
- Use the intended DSA pattern when brute force would scale poorly.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 3 hidden categories
Classic overlap
Input[[1,3],[2,6],[8,10],[15,18]]
Expected[[1, 6], [8, 10], [15, 18]]
Only the first two intervals overlap.
Touching endpoints
Input[[1,4],[4,5]]
Expected[[1, 5]]
Touching intervals merge in this prompt.
Hidden Test Categories
empty interval listalready merged intervalsfully nested intervals