← Lessons

quiz vs the machine

Silver1040

Algorithms

Representing Intervals

How a start and end pair models a span of time, space, or value.

4 min read · intro · beat Silver to climb

What an interval is

An interval is a contiguous span captured by two numbers: a start and an end. Booking 9 to 11 on a calendar, the pixels from 100 to 250, or the values from 3 to 8 are all intervals. We usually write them as a pair such as start equals 3, end equals 8.

Open versus closed

The boundary convention matters.

  • Closed intervals include both endpoints, so 3 to 8 contains 3 and 8.
  • Half open intervals include the start but exclude the end, written start up to but not including end. This is popular because adjacent ranges like 0 to 5 and 5 to 10 tile cleanly without overlap.

Pick one convention for a whole problem and never mix them, or you will create off by one bugs at the seams.

When two intervals touch

Two intervals overlap when each starts before the other ends. A common test is start of A is less than end of B and start of B is less than end of A. If you only need them to merely touch at a point, relax the comparison to less than or equal.

Why representation is the foundation

Almost every interval algorithm sorts by start or by end, then walks the list. Choosing a clear pair and a fixed boundary rule makes that walk reliable.

Key idea

An interval is just an ordered start and end pair; commit to one boundary convention before you compute anything.

Check yourself

Answer to earn rating on the learn ladder.

1. Why are half open intervals popular for tiling ranges?

2. Two closed intervals A and B overlap when which condition holds?