Interval problems share a shape you'll recognize immediately once you've seen it: sort the intervals by some key, then make a single linear pass. The entire skill in this topic is picking the right sort key and the right thing to track during that pass — get those two decisions right and the rest nearly writes itself.
Sort by start: building up a result
Merge Intervals is the cleanest version of this shape: sort by start, then walk the list extending a "current" merged interval whenever the next one overlaps it, or closing it and starting a new one when it doesn't. Insert Interval is the same idea applied to inserting one new interval into an already-sorted list in a single O(n) pass — copy everything strictly before it, absorb everything it overlaps, copy everything strictly after.
Sort by end: greedy selection
Non-overlapping Intervals and Minimum Number of Arrows to Burst Balloons both need the *other* sort key. This is the classic "activity selection" greedy: when two intervals conflict, keep whichever one ends earlier — it leaves strictly more room for everything that comes after it than any other choice would. Sorting by start instead is the natural first instinct, and it's provably wrong: a long interval that happens to start early can look worth keeping, when discarding it would have left more room overall.
Before writing any interval solution, ask: does correctness here depend on which interval starts first, or which one finishes first? Merging and inserting care about start order. Greedy selection — picking the maximum number of non-conflicting intervals — almost always cares about end order instead.
Sweep line: counting what's active at once
Meeting Rooms II answers "what's the peak number of intervals overlapping at any single instant?" by splitting every interval into two separate events — a start and an end — sorting all of them together, and sweeping through in order while tracking a running count. A start increments the count, an end decrements it, and the maximum value the count ever reaches is the answer. Car Pooling is the exact same sweep, just counting passengers instead of meetings — and it adds one more wrinkle: at a tied location, a drop-off (decrement) must be processed before a pick-up (increment), since a passenger leaving frees a seat before a new one at that same stop needs it.
Two pointers across two separate lists
Interval List Intersections merges two already-sorted, already-non-overlapping interval lists with two pointers instead of one: compute the overlap between the current interval from each list, record it if valid, then advance whichever one ends first — it can't possibly overlap anything further along in the other list either, so it's safe to move past.
When a sorted structure beats scanning everything
My Calendar I checks a new booking against every previously booked event — done naively, that's O(n) work per booking. Keeping the booked events in a std::set ordered by start time turns each check into O(log n): lower_bound finds the one neighboring event on each side that could possibly conflict, without ever looking at anything else. Minimum Interval to Include Each Query pushes this further, combining a sort with a min-heap (the same "keep only what's still relevant" idea as heaps from that earlier topic) to answer every query in O(log n) amortized instead of rechecking every interval per query.
Common mistakes
- Sorting by start when the greedy argument needs end — Non-overlapping Intervals and Minimum Arrows both silently produce a wrong (too-large) count if sorted by start instead.
- Missing a required tie-break — Remove Covered Intervals needs same-start intervals sorted by end descending, or a covering interval can be processed after (instead of before) the smaller interval it covers, undercounting how many get removed.
- Assuming one consistent rule for "touching" boundaries — Merge Intervals treats a touching pair like
[1,4],[4,5]as overlapping (they merge); Meeting Rooms treats a touching pair as perfectly fine (back-to-back, no conflict). Each problem defines its own boundary semantics — read it, don't assume it. - Forgetting same-location event ordering — Car Pooling's drop-offs must be processed before pick-ups at an identical location, or a valid schedule gets rejected on a transient (never-actually-true) capacity spike.
Takeaways
- Nearly every interval problem is "sort by the right key, then one linear pass" — identifying the right key is most of the work.
- Merging/inserting sorts by start; greedy maximum-selection sorts by end (the activity-selection argument).
- Counting concurrent overlap is a sweep over start/end events, with same-location ties sometimes needing a specific processing order.
- Two sorted interval lists merge with two pointers, each advancing past whichever interval ends first.
- A sorted structure (ordered set, or a heap alongside sorted queries) turns a per-check O(n) scan into O(log n) when checks happen repeatedly.
Try it: sort by start, build a result
Merge Intervals — the foundational shape every other problem in this topic riffs on.
Loading starter code…
Try it: sort by end, greedy selection
Non-overlapping Intervals — the exercise that makes the start-vs-end sort key distinction concrete. Trace through the "wrong greedy" test case by hand before you look at the hint.
Loading starter code…
Try it: sweep line concurrency counting
Meeting Rooms II — the clearest example of splitting intervals into separate start/end events and sweeping through them.
Loading starter code…
That's 3 of the 12 problems in this topic. Car Pooling is worth doing right after Meeting Rooms II — it's the same sweep-line idea with one extra same-location ordering trap layered on. See the full Intervals set →
Checkpoint · Intervals
5 questions · pass at 70%
Finished Intervals?
Pass the quiz to complete it automatically.