time ranges
overlap detection
date comparison
programming logic
duplicate question

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:

text
start_a < end_b and start_b < end_a

That formula is concise and covers every case where the ranges share time.

Equivalent Python example:

python
1from datetime import datetime
2
3
4def overlaps(start_a, end_a, start_b, end_b):
5    return start_a < end_b and start_b < end_a
6
7
8a_start = datetime(2025, 1, 1, 9, 0)
9a_end = datetime(2025, 1, 1, 10, 0)
10b_start = datetime(2025, 1, 1, 9, 30)
11b_end = datetime(2025, 1, 1, 11, 0)
12
13print(overlaps(a_start, a_end, b_start, b_end))

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:

python
return start_a < end_b and start_b < end_a

If touching endpoints should count as overlap, use inclusive logic:

python
return start_a <= end_b and start_b <= end_a

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.

python
1def numeric_overlap(a_start, a_end, b_start, b_end):
2    return a_start < b_end and b_start < a_end
3
4print(numeric_overlap(8, 10, 9, 11))
5print(numeric_overlap(8, 10, 10, 12))

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.

python
def validate_range(start, end):
    if start > end:
        raise ValueError("start must not be after end")

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:

  1. validate each range,
  2. choose whether touching endpoints count,
  3. apply the corresponding two-comparison formula,
  4. 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.

Course illustration
Course illustration

All Rights Reserved.