Pattern

Interval scheduling

Sort a list of ranges by an endpoint, then sweep once to merge, count overlaps, or decide what to keep.

What it is

Interval problems give you a collection of ranges — meetings, busy blocks, coverage windows — and ask you a question that depends on how they overlap: how many merge into one, how many rooms or resources you need at once, which ones to keep or discard. The technique that unlocks nearly all of them is the same: sort by an endpoint (usually start, sometimes end), then make a single sweep, carrying forward just enough state (a running merged interval, a count of active ranges, a heap of end times) to answer the question without re-comparing every pair.

When it applies

Tells in the problem

The classic mistake

The mistake interviewers report most is skipping or mishandling the sort, or getting the overlap comparison backwards for the interval's endpoint convention (closed [a,b] vs. half-open [a,b)), so intervals that should merge don't, or ones that shouldn't get merged do. It happens because the sweep logic itself is short and easy to write correctly once the input is sorted the right way — so the actual bug hides in the one-line comparison (< vs <=) that decides "do these touch or overlap," which differs from problem to problem based on the stated convention. Before coding, write out on paper what happens at a shared endpoint for this specific problem, and pick your comparison operator to match it exactly.

Questions to start with

All 9 interval scheduling questions

Problem statement, examples and hints are free on every page; the worked approach, solution and tests are in the company guides.

Free starter pack

13 must-tier questions with full solutions

Sign in with an email — no purchase — and read them in the full reader.

No password, no spam. One sign-in email; you can read the pack in the full reader.