Problems › TypeScript › events
Do two bookings clash
A calendar checker takes two half-open intervals and reports whether they overlap.
- Each interval is [start, end) — the end moment is excluded.
- An interval whose end is not after its start is empty and clashes with nothing.
bookingsOverlap(firstStart: int, firstEnd: int, secondStart: int, secondEnd: int) → bool
Where you start
function bookingsOverlap(firstStart: number, firstEnd: number, secondStart: number, secondEnd: number): boolean {
}
Worked examples
| Call | Result |
|---|---|
bookingsOverlap(10, 20, 15, 25) | true |
bookingsOverlap(10, 20, 20, 30) | false |
bookingsOverlap(10, 20, 25, 30) | false |
bookingsOverlap(10, 30, 15, 20) | true |
Hint
Two non-empty intervals overlap when each starts before the other ends.
Reference solution in TypeScript
function bookingsOverlap(firstStart: number, firstEnd: number, secondStart: number, secondEnd: number): boolean {
if (firstEnd <= firstStart || secondEnd <= secondStart) return false;
return firstStart < secondEnd && secondStart < firstEnd;
}