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
Java 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
boolean minimumGap(List<Slot> slots, int gapMinutes) {
}
Worked examples
| Call | Result |
|---|---|
minimumGap(Main.<Slot>ls(new Slot(1, 5), new Slot(10, 15)), 5) | true |
minimumGap(Main.<Slot>ls(new Slot(1, 5), new Slot(8, 12)), 5) | false |
minimumGap(Main.<Slot>ls(new Slot(1, 5), new Slot(15, 20), new Slot(25, 30)), 5) | true |
minimumGap(Main.<Slot>ls(new Slot(1, 5)), 10) | true |
Hint
Walk the list once, checking the gap between each pair of neighbours.
Reference solution in Java
boolean minimumGap(List<Slot> slots, int gapMinutes) {
for (int i = 1; i < slots.size(); i++) {
if (slots.get(i).start - slots.get(i - 1).end < gapMinutes) return false;
}
return true;
}