Skip to content
AITroveRead. Build. Understand.
Make this comfortable

Python exercise: validate half-open reservations before accepting a batch

Last updated: 30 Sept 20264 min read
tutorial
IntermediateBy AITrove Editorial

Two half-open intervals conflict when each starts before the other ends.

Download Python source kit

Operation contract

The room schedule accepts integer minute pairs within one day, sorts a copy and rejects overlapping neighbors. Touching endpoints are allowed: a reservation ending at minute 60 can be followed by one starting at 60. Sorting a copy preserves the caller order. Validation happens before sorting, so malformed pairs cannot be mistaken for valid time points.

Failure and ownership boundary

The function reports only a Boolean and does not allocate rooms or choose a maximum weighted schedule. Empty and single-entry schedules are conflict-free by this rule. A real calendar needs timezone, identity, recurrence and concurrent-write rules; Python weighted interval scheduling: reconstruct the accepted nonoverlapping jobs solves a different optimization problem.

Working program

python
def no_conflicts(reservations):
    if type(reservations) is not list or len(reservations) > 200:
        raise ValueError("reservation cap")
    for span in reservations:
        if type(span) is not tuple or len(span) != 2 or any(type(minute) is not int for minute in span) or not 0 <= span[0] < span[1] <= 1440:
            raise ValueError("half-open minute pair")
    ordered = sorted(reservations)
    return all(previous[1] <= current[0] for previous, current in zip(ordered, ordered[1:]))

print(no_conflicts([(60, 90), (0, 60)]))
print(no_conflicts([(60, 90), (89, 100)]))
print(no_conflicts([]))
try:
    no_conflicts([(True, 60)])
except ValueError:
    print("boolean minute rejected")

Output

Output
True
False
True
boolean minute rejected

Costs and limits

Sorting n intervals costs O(n log n) time and O(n) copied references; the neighbor scan is O(n). The 200-item gate controls this fixture, but the integer-minute model deliberately excludes dates and timezone rules.

Common Mistakes

  • Touching half-open endpoints do not overlap.
  • Do not mutate the caller schedule merely to check it.

Connected lessons

Python weighted interval scheduling: reconstruct the accepted nonoverlapping jobs, Python range exercise: immutable prefix totals and exclusive endpoints, Python datetime: require an offset before comparing timestamps.

Trace the next boundary

Python exercise: merge overlapping half-open reservations.

python
interval-conflict-exercise
Storage details