Interview patterns
Intervals Pattern Explained: Sort by Start, Then Merge in One Pass
Updated Dec 1, 2026 · 3 min read
Sort the [start, end] pairs by start, then walk them once. If the next start is at or before the current end, the two overlap, so stretch the current end to the larger of the two ends. If not, start a new interval. The sort costs O(n log n) and the walk is O(n), instead of comparing every pair in O(n²).

Key points
- ●Spot it when the input is a list of [start, end] pairs and the question is about overlaps, merging, free time, rooms or arrows.
- ●Sort by start first, so any overlap is always with the last interval you kept.
- ●Overlap when next start <= current end: set the end to max(current end, next end).
- ●No overlap: append the next interval as a new block.
- ●Practice on 56, 57, 435, 252 and 253.

The problem: which meetings overlap?
You get four meetings as [start, end] pairs: [1,3], [8,10], [2,6], [15,18]. Some of them overlap, and you want the busy blocks of the day with every overlap merged.
The first idea most people have is to compare every meeting with every other one. Four meetings already means 6 pair checks, and the count grows as n squared: a thousand meetings is about half a million pairs. Worse, merging two meetings can create a new overlap with a third, so a single round of pair checks isn't even enough.
When to spot it
The "When to spot it" table on the visual sheet above lists the clues:
- The input is a list of [start, end] pairs.
- The question is about overlaps or merging.
- A scheduling twist: find the free time, or how many rooms you need.
- A range twist: how many arrows it takes to cover the ranges.
If you catch yourself writing a double loop over pairs of ranges, that's the sign to sort first.
The idea: sort, compare, choose
The sheet breaks it into three steps.
- Sort the intervals by start time.
- Compare the next start with the end of the interval you're currently building.
- Choose the transition: extend the current interval when they overlap, otherwise start a new one.
Sorting is what makes the single pass work. Once everything is in start order, a new interval can only overlap the last block you kept. Every earlier block ends before that one starts, so there's nothing else to check.
The state-transition table on the sheet says it in two rows:
| Condition | Meaning | What you do |
|---|---|---|
| next_start <= current_end | Intervals overlap | current_end = max(current_end, next_end) |
| next_start > current_end | No overlap | append [next_start, next_end] |
Worked example
Start with [1,3], [8,10], [2,6], [15,18]. Sorted by start, it becomes [1,3], [2,6], [8,10], [15,18].
| Step | Next | Current block | Start <= end? | Output after the step |
|---|---|---|---|---|
| start | [1,3] | [1,3] | first one | [[1,3]] |
| 1 | [2,6] | [1,3] | 2 <= 3, yes | [[1,6]] |
| 2 | [8,10] | [1,6] | 8 <= 6, no | [[1,6], [8,10]] |
| 3 | [15,18] | [8,10] | 15 <= 10, no | [[1,6], [8,10], [15,18]] |
The answer is [[1,6], [8,10], [15,18]]. Three comparisons after the sort, no pair checks.
The Python template
def merge(intervals):
intervals.sort(key=lambda x: x[0])
out = [intervals[0]]
for s, e in intervals[1:]:
if s <= out[-1][1]:
out[-1][1] = max(out[-1][1], e)
else:
out.append([s, e])
return out
The invariant to keep in your head: the last interval in out is always the block you're currently merging into. Every line of the loop either stretches that block or closes it by appending a new one.
Complexity
The sort is O(n log n) and the walk is O(n), so the whole thing is O(n log n). That beats the O(n²) of comparing every pair. The output can hold up to n intervals when nothing overlaps, so it's O(n) extra space.
The traps
- Empty input. The template reads
intervals[0], so check that there's at least one interval first. - Touching ends. With
<=, [1,3] and [3,5] merge into [1,5]. If the problem says touching meetings don't conflict, switch to<. - Forgetting max. If you set the end to the next interval's end, [1,10] followed by [2,4] shrinks to [1,4]. Always take the larger end.
Problems to practice
The classic problems list on the sheet:
- 56 Merge Intervals: the template above, as is.
- 57 Insert Interval: add one new interval, then merge.
- 435 Non-overlapping Intervals: count how many you'd have to remove so none overlap.
- 252 Meeting Rooms: can one person attend every meeting? Sort, then check for any overlap.
- 253 Meeting Rooms II: how many rooms do you need at the same time?
Frequently asked questions
Why do you sort intervals by start time?
Once the intervals are in start order, a new interval can only overlap the last one you kept. That turns a check against every earlier interval into one comparison per step, so a single pass is enough.
What is the time complexity of merge intervals?
O(n log n) for the sort plus O(n) for the walk, so O(n log n) overall. The output list takes O(n) space in the worst case, when nothing overlaps.
Do intervals that only touch count as overlapping?
It depends on the problem. With s <= out[-1][1], [1,3] and [3,5] merge into [1,5]. If touching ends should stay separate, use s < out[-1][1] instead. Read the problem statement for which one it wants.
Why use max for the new end instead of the next interval's end?
Because the next interval can sit entirely inside the current one. Merging [1,10] with [2,4] has to keep 10 as the end. Taking max(current end, next end) handles both cases.