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
C++ 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
bool minimumGap(std::vector<Slot> slots, int gapMinutes) {
}
Worked examples
| Call | Result |
|---|---|
minimumGap(std::vector<Slot>{Slot{1, 5}, Slot{10, 15}}, 5) | true |
minimumGap(std::vector<Slot>{Slot{1, 5}, Slot{8, 12}}, 5) | false |
minimumGap(std::vector<Slot>{Slot{1, 5}, Slot{15, 20}, Slot{25, 30}}, 5) | true |
minimumGap(std::vector<Slot>{Slot{1, 5}}, 10) | true |
Hint
Walk the list once, checking the gap between each pair of neighbours.
Reference solution in C++
bool minimumGap(std::vector<Slot> slots, int gapMinutes) {
for (size_t i = 1; i < slots.size(); i++) {
if (slots[i].start - slots[i - 1].end < gapMinutes) return false;
}
return true;
}