Determining if two time ranges overlap at any point
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Two time ranges overlap if they share any instant in common. The easiest correct solution is not to list all possible overlap cases one by one, but to define what “non-overlap” means and negate it. The only subtle part is deciding whether touching endpoints count as overlapping, because that changes the comparison operators.
The Core Overlap Rule
For half-open intervals like [start, end), two ranges overlap when:
That formula is concise and covers every case where the ranges share time.
Equivalent Python example:
This returns True because the intervals share time between 9:30 and 10:00.
Why The Formula Works
It is often easier to think about the opposite. Two ranges do not overlap only if one ends before the other begins.
Non-overlap means either:
- '
end_a <= start_b,' - or
end_b <= start_a.
Negating that gives the common overlap test. This is why the formula is more reliable than writing several case-by-case conditions.
Touching Endpoints: Do They Count?
This is the main design choice.
If 10:00-11:00 and 11:00-12:00 should not count as overlapping, use half-open logic:
If touching endpoints should count as overlap, use inclusive logic:
Neither choice is universally correct. The business rule determines which comparison belongs in the code.
A Simpler Numeric Example
The same logic works for plain numbers, not just dates.
This makes it easier to understand the logic before applying it to timestamps.
Validate Input First
A range should itself be valid before you ask whether it overlaps another range.
Without this check, your overlap function may still return an answer, but the answer would be based on invalid interval data.
Time Zones Matter In Real Systems
If your timestamps include time zones, normalize them before comparison. Comparing naive and aware datetimes, or comparing values from different zones without normalization, can produce errors or misleading results.
For business systems, the real bug is often not the overlap formula itself. It is inconsistent timestamp representation.
A Good Rule Of Thumb
Use this workflow:
- validate each range,
- choose whether touching endpoints count,
- apply the corresponding two-comparison formula,
- normalize time zones if dates are zone-aware.
That covers most scheduling, booking, and availability problems cleanly.
Common Pitfalls
- Writing many separate overlap cases instead of using the compact formula.
- Forgetting to decide whether touching endpoints count as overlap.
- Comparing invalid ranges where start is after end.
- Mixing time-zone-aware and naive timestamps.
- Treating date-only ranges and timestamp ranges as if they had the same boundary semantics.
Summary
- Two ranges overlap when each starts before the other ends.
- For half-open intervals, use
start_a < end_b and start_b < end_a. - If touching endpoints should count, switch to inclusive comparisons.
- Validate the intervals before comparing them.
- Most overlap bugs come from boundary semantics and time representation, not from the formula itself.

