Implement a meeting scheduler that stores meetings as half-open intervals [start, end), where start and end are integer timestamps and start < end.
The scheduler must support:
-
book(start, end) -> bool
: Add the meeting if it does not overlap with any existing meeting. Return
true
if the meeting is booked, otherwise return
false
and leave the schedule unchanged.
-
remove_before(time) -> int
: Given a cutoff timestamp, remove all meetings that end at or before
time
. Return the number of removed meetings.
-
get_meetings() -> List[Tuple[int, int]]
: Return all currently scheduled meetings sorted by start time.
Follow-up discussion:
-
What data structure would you use to optimize
book
and
remove_before
?
-
How would the complexity change if you used an unsorted list, a sorted array, or a balanced binary search tree?
-
What edge cases should be covered by tests?
Example:
book(10, 20) -> true
book(20, 30) -> true # Adjacent meetings are allowed.
book(15, 25) -> false # Overlaps with [10, 20).
book(5, 10) -> true
remove_before(20) -> 2 # Removes [5, 10) and [10, 20).
get_meetings() -> [(20, 30)]
Assume the number of meetings can be large, so the solution should be efficient.