Skip to content
Tech Interview Prep
Technical interview guide

Intervals

Ranges with a start and end: sorting by start (or end) turns overlap and merge problems into a single linear pass.

Minutes to read
4
Practice MCQs
25
Interview Q&A
25
Edition
v8
Editorial status
Reviewed

Scope: Language-neutral, with sorting and heap behaviour cited from CPython 3.14.

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

overlap(a,b)  ⟺  as<be∧bs<ae\mathrm{overlap}(a,b) \iff a_s < b_e \land b_s < a_e cover(x)=#{i:li≤x}−#{i:ri≤x}(half-open)\mathrm{cover}(x)=\#\{i: l_i \le x\}-\#\{i: r_i \le x\}\quad\text{(half-open)}
  • 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

  1. Fix the convention first. It sets every strict or non-strict comparison.
  2. Sort by start for merging and insertion, and by end for greedy selection.
  3. Walk once. Extend the running block while the next start is below the running end, taking the max of the two ends.
  4. Otherwise emit the running block and open a new one.
  5. 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.
yes no Sort by start next.start < running.end? running.end = max of both ends emit running block, open new block

When to use and when not to

WorkloadUseAvoid
Static set, many queriesSort once, then sweep or binary searchAny tree
Inserts and deletes with coverage countsBalanced tree keyed on start, augmented with max end, or two order-statistic treesRebuilding the sorted list on every change
Range add with range min or sumSegment tree with lazy propagationFenwick tree for a minimum
Point updates with prefix sumsFenwick treeSegment tree when no range updates are needed
Data larger than RAMExternal sort of events, then one streaming passAn in-memory list of all intervals

Traps interviewers probe

  1. Closed comparison on half-open data. The check a.start <= b.end and b.start <= a.end returns True for [540,600) and [600,660), which should not conflict. Fix: compare strictly on both sides, which returns False.
  2. 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.
  3. 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.
  4. 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.
  5. Dropping the trailing gap. free_slots on 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_start reports 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.