Practice Discrete Math

Algorithms / Intervals On Lines

Least You Need to Know: Intervals on Lines, Overlap Tests, and Merge Reasoning

A large class of interview problems is really one-dimensional geometry in disguise. Once you model each object as an interval, the key moves are overlap tests, sorting by start point, and carrying the rightmost covered endpoint as an invariant.

The least you need to know

Key notation

[l, r] closed interval from `l` to `r` inclusive
max(l1, l2) later starting point
min(r1, r2) earlier ending point

Tiny worked example

  • For intervals `[1, 4]` and `[3, 6]`, the later start is `3` and the earlier end is `4`, so they overlap.
  • After sorting by start, you can merge them into `[1, 6]` because the second start is inside the current covered range.

Common mistakes

How to recognize this kind of problem

Start practice