Overview
Curated: · Written: · Reviewed:
Intervals
In one line
Ranges with a start and end. Sort by start to merge or insert in one pass, sort by end to select the most non-overlapping ones, and reach for a tree only when the set changes between queries.
Key definitions
- Half-open [start, end): includes start, excludes end, so [0, 10) and [10, 20) touch without overlapping.
- Overlap test (half-open): a.start < b.end and b.start < a.end.
- Peak concurrency: the most intervals covering one point. It equals the minimum number of resources.
- Coverage count: the number of stored intervals containing a query point.
- Augmented max end: the largest end in a subtree, stored in an interval tree node so queries can prune.
- Lazy propagation: a pending range update held at a segment tree node and pushed to children on descent.
Formulas and complexity
- Sort then sweep: O(n log n) for the sort, O(n) for the pass.
- Min-heap of end times: O(n log n), and it yields the room assignment.
- Interval tree: O(log N) per insert or delete, O(log N + k) per overlap query.
- Two order-statistic trees (starts and ends): O(log N) per update and per coverage count.
- Segment tree with lazy propagation: O(log n) per range update or range query.
- Fenwick tree: O(log n) per update or prefix query, for invertible aggregates only.
How it works
- Fix the convention first. It sets every strict or non-strict comparison.
- Sort by start for merging and insertion, and by end for greedy selection.
- Walk once. Extend the running block while the next start is below the running end, taking the max of the two ends.
- Otherwise emit the running block and open a new one.
- For rooms, pop every heap end at or before the new start, then push the new end. The peak heap size is the room count.
When to use and when not to
| Workload | Use | Avoid |
|---|---|---|
| Static set, many queries | Sort once, then sweep or binary search | Any tree |
| Inserts and deletes with coverage counts | Balanced tree keyed on start, augmented with max end, or two order-statistic trees | Rebuilding the sorted list on every change |
| Range add with range min or sum | Segment tree with lazy propagation | Fenwick tree for a minimum |
| Point updates with prefix sums | Fenwick tree | Segment tree when no range updates are needed |
| Data larger than RAM | External sort of events, then one streaming pass | An in-memory list of all intervals |
Traps interviewers probe
- Closed comparison on half-open data. The check
a.start <= b.end and b.start <= a.endreturns True for [540,600) and [600,660), which should not conflict. Fix: compare strictly on both sides, which returns False. - Sorting by end before merging. A later-starting interval can arrive before one it overlaps. Fix: sort by start to merge. For selection, sort by end: on [1,10], [2,3], [4,5], [6,7], sorting by start keeps one meeting and sorting by end keeps three.
- Assigning the next end instead of the max. Merging [1,10] with [2,3] by assignment gives [1,3]. Fix: take max of the two ends.
- Using a Fenwick tree for maxima. A decrement cannot be undone, so stale values remain. Fix: a segment tree with point updates for range max.
- Dropping the trailing gap.
free_slotson busy (9,11), (13,14), (16,17) with lo 8 and hi 18 returns [(8,9),(11,13),(14,16)] and misses (17,18). Fix: after the loop, append (cur, hi) when cur < hi.
Interview tips
Open with the convention, then the sort key, then the complexity: half-open, so touching is not a conflict; sort by start, walk once, take the max end.
Follow-ups to expect:
- Touching and zero-length intervals.
- Peak count versus assignment: the heap gives the rooms and its peak matches the peak overlap.
- Memory: 2 billion intervals on 8 GB. Use an external sort of events and a streaming counter, so only the counter stays in memory.
- Time zones: 2026-10-25 00:30 to 02:30 in Europe/London gives a naive difference of 2 hours, but the real elapsed time is 3 hours. Store instants and render in a zone.
- Floating point: 0.1 + 0.2 is above 0.3, so
a_end > b_startreports overlap for touching intervals. Use integers or fixed-point endpoints. - Rate limiter: appending every attempt, rejected ones included, leaves about 3,000,000 entries after 50,000 requests per second for 60 s. Store accepted timestamps only, or use buckets.
30-second outline: Half-open means touching intervals do not conflict, so I compare strictly on both sides. I sort by start and walk once, extending the running block while the next start is below its end and taking the max end. For rooms, a min-heap of end times with pop-while-expired gives the count, which equals peak overlap. If the set changes between queries, I use an augmented tree at O(log N) per operation.
Feedback on this overview
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.