Merge intervals problems all share one underlying object: a collection of ranges, each with a start and an end, where the interesting questions are about overlap — which ranges touch, how many overlap at once, what’s left after combining or removing them.
The pattern’s power comes from a single observation that simplifies almost every problem in this family: once intervals are sorted by start time, you only ever need to compare each interval against the most recently processed one to know whether it overlaps, rather than checking it against every other interval. That turns an apparently quadratic comparison problem into a linear scan after one sort.
The clearest signal for this pattern is a problem involving ranges — time windows, numeric spans, booking slots — where the core question is about overlap, merging, or gaps between them. A second signal is a request to determine how many ranges are simultaneously active at some point, usually meaning the maximum overlap count across all intervals, a variant that benefits from treating starts and ends as separate, independently sortable events rather than intervals as indivisible units. A third signal is two separate sets of ranges compared against each other, such as finding where two schedules agree — a two-pointer walk over intervals rather than a single sort-and-sweep. A fourth signal, bridging into dynamic programming, is intervals carrying an associated weight, with a request to select a non-overlapping subset maximizing total value; that combination of “interval structure” plus “optimization over choices” turns a pattern-library problem into a DP problem that leans on interval sorting for its setup.
This pattern does not apply when the ranges aren’t independently orderable by a single start/end pair — multi-dimensional ranges, rectangles for instance, need different tools — and it doesn’t help when the question isn’t really about overlap at all, even if the input happens to be described using start/end pairs; if you don’t need to know which ranges touch, sorting by start won’t buy you anything.
Continue with the book
Explore the full treatment, worked examples, and exercises in Algorithmic Edge.
Companion article for Algorithmic Edge, published by Choral Software. Article text and original examples © 2026 Choral Software. All rights reserved.
