Two half-open intervals conflict when each starts before the other ends.
Python exercise: validate half-open reservations before accepting a batch
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
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
True
False
True
boolean minute rejectedCosts 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.
