Merge Intervals
Sort by start. Walk through the intervals keeping a list merged. If the current interval starts at or before the end of merged.back(), it overlaps that group: stretch the group's end to max(end, cur.end). Otherwise it starts a new group.
cur joins the last group if and only if cur.start ≤ merged.back().end.
Problem
You get a list of intervals. Each one is [start, end] and includes both ends.
If two intervals overlap, join them into one bigger interval. Keep doing this until no two intervals overlap. Return the final list.
Intervals that only touch (one ends exactly where the next starts, like [1,4] and [4,5]) also count as overlapping and must be joined.
A list intervals, where intervals[i] = [start, end].
The merged intervals, with no two overlapping. Any order is accepted.
[1,3] and [2,6] overlap (2 is inside [1,3]), so they become [1,6]. The other two don't touch anything.
They share the point 4, which counts as overlapping.
The input is not sorted. [1,4] and [4,7] touch at 4, so they join.
- • 1 ≤ intervals.length ≤ 10^4
- • intervals[i].length == 2
- • 0 ≤ start ≤ end ≤ 10^4
First, think yourself
Before opening any solution, try to answer these on paper. Use hints only when stuck — each hint is counted.
- 1.
When exactly do two intervals
[a, b]and[c, d]overlap? Write the test with≤or<and check it on touching intervals. - 2.
If the input were sorted by start, where would the intervals that overlap a given one be?
- 3.
When I join two intervals, what is the new start and the new end?
- 4.
Can one interval sit completely inside another? What happens to the end then?
- 5.
Once an interval starts after my current group ends, can any later interval still join that group?
Brute force approach
Optimized approach
Why does this algorithm work?
Dry run
All approaches compared
| Approach | Idea | Time | Space |
|---|---|---|---|
| Brute Force | Scan all pairs, merge the first overlapping pair found, repeat | O(N³) | O(N) |
| Optimal | Sort by start; each interval either stretches the last group or starts a new one | O(N log N) | O(N) |
Why does this belong to this pattern?
- “list of [start, end] intervals”Interval problem → think about sorting by start or by end.
- “merge all overlapping”Groups of intervals that chain together. After sorting by start, a group is a run of neighbours.
- “any order is fine”We are free to sort the input; nothing depends on the original order.
This is the anchor for the whole Merge sub-pattern: sort by start, keep one running group, and decide with a single test cur.start ≤ end. The next questions change only small parts: Insert Interval skips the sort because the input is already sorted, Remove Covered Intervals changes the test to containment, and Count Days Without Meetings adds up the merged lengths.
Don't make this mistake
The previous interval can be short while the group is long. In [1,10], [2,3], [5,6], the interval [5,6] does not overlap [2,3], but it does overlap the group [1,10]. Always compare with the group, not the neighbour.
An interval can sit completely inside the group. With [1,6], [4,5], writing end = 5 shrinks the group, and a later [6,7] would wrongly become its own group.
Here touching intervals like [1,4] and [4,5] must join. With < they stay apart.
Sorting by end is the trick for the Coverage sub-pattern (Non-overlapping Intervals, Arrows), where you greedily keep the earliest finisher. For merging, sort by start: a long interval like [1,10] sorted by end comes after [2,3] and [5,6]. Those two become separate groups first, then [1,10] only joins the last one, giving the wrong answer [[2,3],[5,10]] instead of [[1,10]].
Want to implement this?
Write it yourself on LeetCode, then come back and compare your code with the solutions above.
Open coding workspaceHow this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Intervals translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.