Is there enough breathing room between slots
A scheduler requires a minimum pause between consecutive bookings. Verify every gap meets the requirement.
- Slots arrive already sorted by start time.
- The gap between two consecutive slots is next.start − prev.end.
- Every such gap must be at least gapMinutes.
- Fewer than 2 slots trivially satisfies the rule.
minimum_gap(slots: list<Slot>, gap_minutes: int) → bool
Where you start
def minimum_gap(slots: list[Slot], gap_minutes: int) -> bool:
Worked examples
| Call | Result |
|---|---|
minimum_gap([Slot(start=1, end=5), Slot(start=10, end=15)], 5) | True |
minimum_gap([Slot(start=1, end=5), Slot(start=8, end=12)], 5) | False |
minimum_gap([Slot(start=1, end=5), Slot(start=15, end=20), Slot(start=25, end=30)], 5) | True |
minimum_gap([Slot(start=1, end=5)], 10) | True |
Hint
Walk the list once, checking the gap between each pair of neighbours.
Reference solution in Python
def minimum_gap(slots: list[Slot], gap_minutes: int) -> bool:
for i in range(1, len(slots)):
if slots[i].start - slots[i - 1].end < gap_minutes:
return False
return True