CS concepts
Interval Problems Explained: Merge, Insert, Meeting Rooms
Updated Dec 2, 2026 · 4 min read
Most interval problems start the same way: sort the [start, end] pairs, then walk them once. Merge sorts by start and stretches the current block with max, removing overlaps sorts by end and keeps whatever finishes first, and meeting rooms keeps a min-heap of end times whose size is the answer. Sorting costs O(n log n) and the walk is O(n), instead of O(n^2) for checking every pair.

Key points
- ●Sort by start and an interval can only clash with the block you're building right now.
- ●Merge Intervals: if the next start is <= the current end, stretch the end with max.
- ●Insert Interval needs no sort: copy the left, absorb the overlap, copy the right, in O(n).
- ●Non-overlapping Intervals: sort by end and keep the interval that finishes first.
- ●Meeting Rooms II: a min-heap of end times; its final size is the number of rooms.

Why interval problems start with a sort
An interval is just a start and an end: a meeting from 1 to 3, a booking from 8 to 10. Unsorted, any interval could clash with any other, so the obvious approach compares every pair. For 10,000 meetings that's about 50 million checks.
Sort by start and something useful happens. An interval can only clash with the block you're building right now. If 8 starts after that block ends at 6, it starts after everything before it too. So you sort once, in O(n log n), and walk once, in O(n). For 10,000 meetings that's about 133,000 steps.
The giveaway: a list of [start, end] pairs and a question about overlaps, merging, free time or how many rooms.
Merge Intervals (LeetCode 56)
Take [1, 3], [2, 6], [8, 10] and [9, 12]. Sort by start, keep an output list, and treat its last block as the one you're still growing. For each interval, ask whether it starts before that block ends.
intervals.sort()
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
[1, 3] goes in. 2 starts before 3 ends, so it becomes [1, 6]. 8 is past 6, so a new block starts, and 9 stretches it to [8, 12]. The answer is [1, 6] and [8, 12]. The max matters: [2, 5] can sit fully inside [1, 10], and without it the block would shrink to 5.
Insert Interval (LeetCode 57)
The blocks [1, 2], [4, 6] and [8, 10] are sorted and don't overlap. A new block [5, 9] arrives. You could append it and merge again, but that re-sorts a list that's already sorted. Instead, split the list into three zones: blocks that end before 5, blocks that clash with [5, 9], and blocks that start after 9.
out, i, n = [], 0, len(intervals)
while i < n and intervals[i][1] < new[0]:
out.append(intervals[i])
i += 1
while i < n and intervals[i][0] <= new[1]:
new = [min(new[0], intervals[i][0]), max(new[1], intervals[i][1])]
i += 1
out.append(new)
return out + intervals[i:]
[1, 2] is copied. [4, 6] is absorbed and the new block grows to [4, 9], then [8, 10] grows it to [4, 10]. The answer is [1, 2] and [4, 10], in one O(n) pass. The visual sheet above walks the same three regions step by step.
Non-overlapping Intervals (LeetCode 435)
Remove the fewest intervals from [1, 3], [2, 4], [3, 5] and [1, 5] so nothing overlaps. Sorting by start is the common mistake: [1, 5] comes first and blocks everything. Sort by end instead and always keep whatever finishes first.
intervals.sort(key=lambda x: x[1])
end, removed = float('-inf'), 0
for s, e in intervals:
if s >= end:
end = e
else:
removed += 1
return removed
| Interval | Start >= last end? | Action |
|---|---|---|
| [1, 3] | yes | keep, end = 3 |
| [2, 4] | 2 < 3 | remove |
| [3, 5] | 3 >= 3 | keep, end = 5 |
| [1, 5] | 1 < 5 | remove |
Two removed. It's the same greedy as fitting the most talks on one stage.
Meeting Rooms I and II (LeetCode 252, 253)
Meetings [9, 11], [9, 12], [10, 13] and [12, 14]. Can one person attend all of them? Sort by start and check whether any meeting starts before the previous one ends. Here [9, 12] clashes right away, so no.
How many rooms? Keep a min-heap of end times, one per room in use. You only care about the room that frees up soonest.
intervals.sort()
rooms = []
for s, e in intervals:
if rooms and rooms[0] <= s:
heapq.heappop(rooms)
heapq.heappush(rooms, e)
return len(rooms)
The heap goes [11], then [11, 12], then [11, 12, 13]. When [12, 14] arrives, the room freed at 11 is reused: pop 11, push 14. Three rooms, because at 10 o'clock three meetings run at once. O(n log n) overall.
Interval List Intersections (LeetCode 986)
Ana is free [1, 4] and [6, 9], Ben is free [2, 7] and [8, 10]. Both lists are sorted, so keep one pointer on each. The overlap of two intervals starts at the later start and ends at the earlier end. If that start isn't past that end, save it. Then move the pointer whose interval ends first.
i = j = 0
out = []
while i < len(A) and j < len(B):
lo = max(A[i][0], B[j][0])
hi = min(A[i][1], B[j][1])
if lo <= hi:
out.append([lo, hi])
if A[i][1] < B[j][1]:
i += 1
else:
j += 1
return out
The result is [2, 4], [6, 7] and [8, 9], in O(n + m) with no sort.
The trap: touching intervals
Do [1, 3] and [3, 5] overlap? In Merge Intervals, yes: [1, 4] and [4, 5] become [1, 5], which is why the test is <=. In Non-overlapping Intervals, [1, 2] and [2, 3] can both stay. In Meeting Rooms, a meeting that ends at 10 frees the room for one that starts at 10. Get the comparison backwards and the code still runs; it just counts one room too many or merges blocks that should stay apart. Test one touching pair by hand before you submit, and don't sort by start out of habit when the problem is about removing intervals.
Frequently asked questions
Why do you sort intervals by start?
Once the list is sorted by start, an interval can only overlap the block you're currently building. If it starts after that block ends, it starts after everything before it too, so one walk is enough. For 10,000 intervals that's about 133,000 steps instead of about 50 million pair checks.
When should you sort intervals by end instead of start?
When you're removing the fewest intervals or keeping the most, as in LeetCode 435. Keeping the interval that finishes first leaves the most room for the rest. Sorting by start can keep a long interval like [1, 5] that blocks everything else.
Do touching intervals like [1, 3] and [3, 5] overlap?
It depends on the problem. In Merge Intervals they merge, so the test is <=. In Non-overlapping Intervals they can both stay, and in Meeting Rooms a room freed at 10 can host a meeting that starts at 10. Read the problem for the touching case before you write the comparison.
What is the time complexity of Meeting Rooms II?
O(n log n). Sorting the meetings is n log n, and each meeting does at most one push and one pop on the heap, each O(log n). The heap holds at most n end times.