Coding interview problems โบ Intervals
๐ Intervals Medium
Merge Intervals
Merge Intervals explained: sort by start, then sweep once and fuse overlaps, O(n log n) tested Python, the touching-endpoints trap and a live timeline visual.
Also searched as: merge overlapping intervals ยท merge intervals sort and sweep ยท combine overlapping ranges
Try it live Free
Merge Intervals & Meeting Rooms
Drag intervals on the timeline and watch overlaps fuse into busy blocks after the sort-by-start, sweep-once move.
Also useful: Sorting Race: Insertion vs Merge vs Quick
The problem
Given a list of intervals [start, end], merge every group of overlapping intervals and return the resulting non-overlapping intervals. Intervals that touch at an endpoint, like [1, 4] and [4, 5], count as overlapping.
The key insight
After sorting by start, any interval that overlaps the current merged block must come right after it. So sweep once: if the next interval starts at or before the current block's end, stretch the end; otherwise close the block and start a new one.
Step-by-step approach
- Sort the intervals by start.
- Put the first interval in the output as the current block.
- For each next interval, if its start is
<=the last block's end, set that end to the larger of the two ends. - Otherwise append the interval as a new block.
- Return the output.
Python solution
def merge(intervals):
out = []
for start, end in sorted(intervals):
if out and start <= out[-1][1]: # overlaps the last block
out[-1][1] = max(out[-1][1], end)
else:
out.append([start, end])
return outComplexity
Sorting dominates. The sweep itself is a single O(n) pass. The output can hold up to n intervals.
Common mistakes
- Setting the end to the new interval's end instead of the max, which breaks on a contained interval like
[1, 10]then[2, 3]. - Using
<instead of<=, so touching intervals are left separate (check what your interviewer wants). - Forgetting to sort first; the single sweep only works on sorted input.
Frequently asked questions
What is the time and space complexity of Merge Intervals?
O(n log n) time for the sort plus O(n) for the sweep, and O(n) space for the output (Python's sort also uses up to O(n) extra).
Why do we sort by start time and not end time?
Sorted by start, every interval that could overlap the current block appears immediately after it, so one comparison with the block's end decides. Sorting by end does not give that guarantee for merging.
How do I handle a stream of intervals arriving one by one?
Keep the merged intervals in a balanced tree or sorted list keyed by start. Each new interval finds its neighbours by binary search and merges with any that overlap.
Preparing for placements?
Take the 2-minute readiness check to find your weak patterns, then follow the placement path from DSA core to system design.
All coding interview problems More Intervals Merge Intervals & Meeting Rooms lab Placement prep guides
Problem statements on this site are written in our own words. Problem names are the common names used across interview-prep communities; BitBootcamp is an independent learning platform and is not affiliated with LeetCode or any company that asks these questions.