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.
minimumGap(slots: list<Slot>, gapMinutes: int) → bool
Go needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.
Where you start
func minimumGap(slots []Slot, gapMinutes int) bool {
}
Worked examples
| Call | Result |
|---|---|
minimumGap([]Slot{Slot{Start: 1, End: 5}, Slot{Start: 10, End: 15}}, 5) | true |
minimumGap([]Slot{Slot{Start: 1, End: 5}, Slot{Start: 8, End: 12}}, 5) | false |
minimumGap([]Slot{Slot{Start: 1, End: 5}, Slot{Start: 15, End: 20}, Slot{Start: 25, End: 30}}, 5) | true |
minimumGap([]Slot{Slot{Start: 1, End: 5}}, 10) | true |
Hint
Walk the list once, checking the gap between each pair of neighbours.
Reference solution in Go
func minimumGap(slots []Slot, gapMinutes int) bool {
for i := 1; i < len(slots); i++ {
if slots[i].Start-slots[i-1].End < gapMinutes {
return false
}
}
return true
}