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
Go 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
func bookingsOverlap(firstStart int, firstEnd int, secondStart int, secondEnd int) bool {
}
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 Go
func bookingsOverlap(firstStart int, firstEnd int, secondStart int, secondEnd int) bool {
if firstEnd <= firstStart || secondEnd <= secondStart {
return false
}
return firstStart < secondEnd && secondStart < firstEnd
}