Problems › JavaScript › dates
Do these two bookings clash
A meeting room calendar refuses a booking that runs into another one. Times are minutes since midnight.
- An interval runs from its start up to but not including its end, so two bookings that merely touch do not clash.
- An interval whose end is not after its start is empty and clashes with nothing.
intervalsOverlap(aStart: int, aEnd: int, bStart: int, bEnd: int) → bool
Where you start
function intervalsOverlap(aStart, aEnd, bStart, bEnd) {
}
Worked examples
| Call | Result |
|---|---|
intervalsOverlap(60, 120, 110, 180) | true |
intervalsOverlap(60, 120, 120, 180) | false |
intervalsOverlap(60, 120, 0, 30) | false |
intervalsOverlap(60, 120, 70, 80) | true |
Hint
Two intervals overlap when each one starts before the other ends. Deal with the empty case first.
Reference solution in JavaScript
function intervalsOverlap(aStart, aEnd, bStart, bEnd) {
if (aEnd <= aStart || bEnd <= bStart) return false;
return aStart < bEnd && bStart < aEnd;
}