Design a meeting room scheduler with conflict detection and recurring weekly bookings
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a meeting room scheduling system that manages bookings across multiple rooms. The system should detect and prevent scheduling conflicts, where two bookings overlap in the same room.
The system must also support recurring weekly events that repeat for a specified number of weeks. Each weekly occurrence is offset by `10080` minutes (7 days x 24 hours x 60 minutes). A recurring booking is only confirmed if **all** of its occurrences are conflict-free; otherwise the entire recurring booking is rejected.
Each successful booking (single or recurring) is assigned a unique booking ID starting from `1`. Canceling a booking removes all of its associated time slots. The system should also support querying a room's full schedule and checking availability for a given time range.
The constructor `MeetingScheduler(roomCount)` sets up rooms numbered `1` through `roomCount`. Implement the following methods:
- `book(room: int, start: int, end: int) -> int`: Book a single meeting in the specified room for the time range `[start, end)`. Returns a unique booking ID on success, or `-1` if the room is invalid or there is a scheduling conflict.
- `bookRecurring(room: int, start: int, end: int, weeks: int) -> int`: Book a recurring weekly meeting starting at `[start, end)` that repeats for the specified number of weeks (each occurrence offset by `10080` minutes). All occurrences share the same booking ID. Returns the booking ID on success, or `-1` if the room is invalid or any occurrence conflicts.
- `cancel(bookingId: int) -> bool`: Cancel all intervals associated with the given booking ID. Returns `True` if the booking was found and removed, `False` otherwise.
- `getSchedule(room: int) -> List[List[int]]`: Return a sorted list of `[start, end]` pairs representing all current bookings in the specified room. Returns an empty list for invalid rooms.
- `isAvailable(room: int, start: int, end: int) -> bool`: Check whether the specified room is available for the time range `[start, end)`. Returns `True` if available, `False` if there is a conflict or the room is invalid.
Example 1, with 3 rooms:
```text
Input:
["MeetingScheduler", "book", "book", "book", "isAvailable", "getSchedule", "cancel", "isAvailable", "getSchedule"]
[[3], [1, 0, 60], [1, 30, 90], [1, 60, 120], [1, 50, 70], [1], [1], [1, 0, 60], [1]]
Output: [null, 1, -1, 2, false, [[0, 60], [60, 120]], true, true, [[60, 120]]]
```
```python
scheduler = MeetingScheduler(3) # 3 rooms available
scheduler.book(1, 0, 60) # -> 1 (room 1 booked [0,60), bookingId=1)
scheduler.book(1, 30, 90) # -> -1 (conflict with [0,60) in room 1)
scheduler.book(1, 60, 120) # -> 2 (room 1 booked [60,120), bookingId=2)
scheduler.isAvailable(1, 50, 70) # -> False (overlaps with both bookings)
scheduler.getSchedule(1) # -> [[0,60],[60,120]]
scheduler.cancel(1) # -> True (removes booking 1: [0,60))
scheduler.isAvailable(1, 0, 60) # -> True (slot is now free)
scheduler.getSchedule(1) # -> [[60,120]]
```
Example 2, with 2 rooms:
```text
Input:
["MeetingScheduler", "bookRecurring", "getSchedule", "book", "cancel", "getSchedule", "book"]
[[2], [1, 100, 200, 3], [1], [1, 10300, 10400], [1], [1], [1, 100, 200]]
Output: [null, 1, [[100, 200], [10180, 10280], [20260, 20360]], 2, true, [[10300, 10400]], 3]
```
```python
scheduler = MeetingScheduler(2) # 2 rooms available
scheduler.bookRecurring(1, 100, 200, 3) # -> 1 (books [100,200), [10180,10280), [20260,20360) in room 1)
scheduler.getSchedule(1) # -> [[100,200],[10180,10280],[20260,20360]]
scheduler.book(1, 10300, 10400) # -> 2 (no conflict, bookingId=2)
scheduler.cancel(1) # -> True (removes all 3 recurring slots)
scheduler.getSchedule(1) # -> [[10300,10400]]
scheduler.book(1, 100, 200) # -> 3 (slot [100,200) is now free again)
```
```hint Overlap test
Write the overlap condition for two half-open intervals and test it on the boundary case from Example 1: a meeting ending at minute 60 and one starting at minute 60.
```
```hint All or nothing
For a recurring booking, decide when you assign the ID and when you write anything into the room. If week 3 conflicts, nothing from weeks 1 and 2 may remain.
```
### Constraints & Assumptions
- `1 <= roomCount <= 100`
- `0 <= start < end <= 10⁶`
- Time values represent minutes from an epoch.
- `1 <= weeks <= 52` for recurring events.
- Booking IDs are integers starting from `1`. Only successful bookings receive an ID, so in Example 1 the failed second call does not use up ID `2`.
- At most `10⁴` total calls across all methods.
### Clarifying Questions to Ask
- Is the end time exclusive, so a meeting ending at minute 60 and one starting at minute 60 can share a room?
- If a recurring meeting is longer than a week, its occurrences overlap each other. Should that count as a conflict?
- Can an occurrence of a recurring booking extend past the `10⁶` bound on input times?
- Should `cancel` return `False` for an ID that was already canceled?
- Will calls arrive from one thread, or do concurrent callers need the conflict check and the insert to happen atomically?
### What a Strong Answer Covers
- The overlap test for half-open intervals, including meetings that only touch at an endpoint.
- All-or-nothing handling of a recurring booking, with one ID shared by every occurrence.
- When booking IDs are assigned, and how cancel finds and removes every slot of a booking.
- Behavior for invalid rooms and repeated cancels across all five methods.
- The time and space cost of each operation, and which data structure would speed up the slow ones.
### Follow-up Questions
- How would you make `cancel` run in time proportional to the booking's own slots instead of scanning every room?
- A room holds tens of thousands of bookings. How would you make the conflict check faster than a linear scan?
- How would you support canceling one occurrence of a recurring series while keeping the rest?
- What changes if many threads call `book` and `cancel` at the same time?
Overview: Implement an in-memory meeting room scheduler that books single and recurring weekly meetings, rejects overlapping bookings, and supports cancel, schedule and availability queries. It tests half-open interval overlap checks, all-or-nothing commits for recurring bookings, and clear class design.