Problems › JavaScript › events
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
Where you start
function minimumGap(slots, gapMinutes) {
}
Worked examples
| Call | Result |
|---|---|
minimumGap([{"start":1,"end":5},{"start":10,"end":15}], 5) | true |
minimumGap([{"start":1,"end":5},{"start":8,"end":12}], 5) | false |
minimumGap([{"start":1,"end":5},{"start":15,"end":20},{"start":25,"end":30}], 5) | true |
minimumGap([{"start":1,"end":5}], 10) | true |
Hint
Walk the list once, checking the gap between each pair of neighbours.
Reference solution in JavaScript
function minimumGap(slots, gapMinutes) {
for (let i = 1; i < slots.length; i++) {
if (slots[i].start - slots[i - 1].end < gapMinutes) return false;
}
return true;
}