Interval Problems: Pattern Recognition Over Memorization
Interval questions look different on the surface, but most reduce to a few repeatable patterns. Learn the decision rules that help you choose quickly and correctly.
Interval problems trick people because they wear different costumes: meetings, bookings, ranges, CPU jobs, timelines, scheduling, free slots.
Under the surface, most of them are the same handful of decisions repeated.
If you can map the problem to the right decision pattern in the first two minutes, the implementation is usually straightforward. If you cannot, you end up guessing between heaps, sweepline maps, sorting, and dynamic programming with no confidence.
This guide is about that first mapping step.
First principles: what an interval question is really asking
Most interval problems ask one of four things:
- Combine: merge overlapping ranges.
- Select: choose maximum compatible intervals.
- Count resources: how many rooms/machines/channels needed at peak.
- Query coverage: what is occupied/free at time
tor over a timeline.
Everything else is variation in constraints.
The decision table you should memorize
Use this table before coding. It picks the right pattern quickly.
| Problem signal | Preferred pattern | Why |
|---|---|---|
| "Merge overlaps" / "insert interval" | Sort by start, one pass merge | Local overlap decisions become linear after sort |
| "Maximum non-overlapping intervals" | Greedy by earliest end | Leaves maximum room for future choices |
| "Minimum intervals to remove" | Same as above + count removals | Equivalent objective from opposite angle |
| "How many rooms at once?" | Min-heap by end times (or sweepline) | Track active intervals efficiently |
| "Many add/remove/query operations" | Ordered map / difference array + sweep | Better for dynamic timeline updates |
If your chosen approach is not justified by one of these signals, pause.
Why sorting by end beats sorting by start (for selection)
For questions like "attend maximum meetings" or "erase minimum overlaps," candidates often sort by start because it feels natural. The optimal greedy choice is usually earliest finishing interval.
Reason: finishing early preserves maximal future capacity. It's a local choice with a global optimality proof (exchange argument).
Interview shortcut explanation:
"If I pick an interval that ends later than another available one, I can swap it with the earlier-ending interval without reducing feasibility, so earliest end is always safe."
That single sentence is high signal.
Rooms/resources: heap vs sweepline
Both can solve peak concurrency. Choose based on needs:
- Min-heap by end time is ideal when iterating sorted starts and tracking active resources.
- Sweepline (events +1/-1) is ideal when you need explicit concurrency over the timeline or many batch queries.
Heap is often simpler in interviews. Sweepline is more expressive when event semantics matter (inclusive/exclusive boundaries, multiple event types).
Boundary semantics cause hidden bugs
Most wrong answers come from endpoint interpretation:
- Is
[start, end]closed or[start, end)half-open? - If one meeting ends at 10 and another starts at 10, do they overlap?
Decide once and encode everywhere consistently. For interview problems, half-open interpretation is common and simplifies adjacency handling.
Say it out loud before coding. It prevents 80% of off-by-one interval mistakes.
Merge pattern in one invariant
For merge intervals, hold this invariant while scanning sorted intervals:
"current is the merged result of all intervals seen so far that overlap the active block."
At each new interval:
- if it overlaps
current, extend end - otherwise, commit
currentand start new block
When candidates panic here, it's usually because they don't separate "active block" from "output list." Keep those distinct and code stays clean.
Weighted intervals: when greedy stops working
If each interval has value/profit and goal is max total value with non-overlap, simple greedy usually fails. This is weighted interval scheduling:
- sort by end
- for each interval, binary search latest compatible predecessor
- DP transition:
dp[i] = max(dp[i-1], value[i] + dp[p(i)])
A lot of candidates force greedy because it's familiar. Interviewers appreciate when you recognize the boundary where DP is required.
Interview narration that improves outcomes
For interval questions, narrate in this sequence:
- classify objective (merge/select/count/query)
- pick pattern and justify with one sentence
- define overlap semantics (
<=vs<) - state complexity after sort (
O(n log n)+ scan/heap)
This makes your approach feel inevitable rather than improvised.
Practice strategy that builds real pattern recognition
Don't batch 10 merge-interval variants in a row. Interleave categories so classification skill gets trained:
- one merge
- one greedy selection
- one room-count heap
- one weighted interval DP
After each solve, write one line: "Signal -> Pattern -> Why." That reflective label is what makes transfer happen in actual interviews.
On sophocode, this is what the coaching flow nudges: not just solving, but tagging the decision signal you used so your next problem starts with faster recognition.
Practice next
- Start with the Intervals practice set.
- Track mistakes in
/dashboardand plan the next block in/roadmap. - SophoCode picks: