Merge Intervals Pattern
The Merge Intervals pattern sorts a list of intervals by start time and walks through them once, merging any interval whose start falls inside the running end of the previous interval. It handles overlap, gap, scheduling, and conflict problems in O(n log n) time dominated by the initial sort.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Complexity
- Time
- O(n log n)
- Space
- O(n)
When to use Merge Intervals
- The input is a list of pairs (start, end), and the answer depends on how they overlap.
- You need to merge overlapping intervals, insert a new interval, or remove the minimum number of intervals to make the rest disjoint.
- You need to find conflict-free meeting room assignments or maximum room usage.
- You need to compute the union, intersection, or symmetric difference of two interval lists.
- You can sort the input by start time as a one-time preprocessing step.
Template
function mergeIntervals(intervals) {
if (intervals.length === 0) return [];
intervals.sort((a, b) => a[0] - b[0]);
const merged = [intervals[0]];
for (let i = 1; i < intervals.length; i++) {
const last = merged[merged.length - 1];
const current = intervals[i];
if (current[0] <= last[1]) {
last[1] = Math.max(last[1], current[1]);
} else {
merged.push(current);
}
}
return merged;
}Example LeetCode problems
| # | Problem | Difficulty | Frequency |
|---|---|---|---|
| 56 | Merge Intervals | medium | foundational |
| 57 | Insert Interval | medium | frequently asked |
| 252 | Meeting Rooms | easy | foundational |
| 253 | Meeting Rooms II | medium | company favorite |
| 435 | Non-overlapping Intervals | medium | frequently asked |
| 452 | Minimum Number of Arrows to Burst Balloons | medium | classic |
| 986 | Interval List Intersections | medium | frequently asked |
| 759 | Employee Free Time | hard | company favorite |
Common mistakes
- Sorting by end time when the algorithm requires start time (or vice versa for the greedy non-overlapping variant).
- Using strict `<` instead of `<=` when checking overlap, which fails for intervals that touch at a single point if the problem treats that as overlapping.
- Mutating the input intervals in place and then returning the wrong reference (the original head is no longer the merged head).
- Building a new array for every merge step in O(n^2) instead of mutating the last element of the result list.
- Forgetting to handle the empty input case, which throws when the algorithm reads `intervals[0]` to seed the loop.
Frequently asked questions
Why sort by start time and not by end time?
Sorting by start time makes overlap detection a single comparison against the running maximum end. The greedy variant of Non-overlapping Intervals sorts by end time instead, because there the goal is to fit the maximum number of disjoint intervals.
How does Meeting Rooms II differ from basic Merge Intervals?
Meeting Rooms II asks for the maximum concurrent count, not the merged list. The standard solution separates the start and end times into two sorted arrays and sweeps them with two pointers, or uses a min-heap of end times.
What if two intervals are exactly back-to-back, like [1,5] and [5,8]?
It depends on whether the problem treats the endpoints as closed or half-open. For closed intervals, they overlap at point 5 and should merge; for half-open they do not. Read the problem statement carefully — picking the wrong comparison loses you the edge-case tests.
Is there a way to do this in less than O(n log n)?
Only if the input is already sorted or the values fit in a small bounded range (allowing radix or counting sort). For general unsorted input, the sort lower bound puts the floor at O(n log n).
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
Practice these live with InterviewChamp.AI
Drill the Merge Intervals pattern under real interview conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →