Design a thread-safe meeting room reservation system with booking and cancellation
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a meeting room reservation system that supports adding rooms, booking time slots, cancelling bookings, and querying availability.
Each reservation is defined by a room id, a start time, and an end time, and covers the half-open interval `[start, end)`. The system must prevent double bookings: a booking attempt that overlaps any existing booking on the same room must fail. Because intervals are half-open, a booking that ends at time `t` does not overlap one that starts at `t`. Booking, cancellation, and lookup operations may be invoked concurrently, so all state mutations must be thread-safe.
Each successful booking is assigned a unique integer booking id, returned to the caller for future cancellation. Ids start at `1` and go up by one with each successful booking across all rooms. A failed attempt does not use up an id, and a cancelled id is never handed out again.
Implement the following methods:
- `addRoom(roomId: int) -> bool`: Register a new meeting room. Returns `True` if added, `False` if the `roomId` already exists.
- `book(roomId: int, start: int, end: int) -> int`: Reserve the interval `[start, end)` on the given room. Returns a unique positive booking id on success, or `-1` if the room does not exist or the slot overlaps an existing booking.
- `cancel(bookingId: int) -> bool`: Cancel an existing booking by its id. Returns `True` if cancelled, `False` if the booking id is unknown (an id that was already cancelled counts as unknown).
- `isAvailable(roomId: int, start: int, end: int) -> bool`: Return `True` if the interval `[start, end)` is free on the given room, `False` otherwise (including when the room does not exist).
- `getBookings(roomId: int) -> List[List[int]]`: Return the active bookings on the room as `[start, end]` pairs, sorted by start time. Returns an empty list if the room has no bookings or does not exist.
Example 1:
```text
addRoom(1) -> True
addRoom(1) -> False
book(1, 10, 20) -> 1
book(1, 15, 25) -> -1
book(1, 20, 30) -> 2
book(2, 0, 5) -> -1
book(1, 0, 10) -> 3
getBookings(1) -> [[0, 10], [10, 20], [20, 30]]
```
Room 1 can only be added once. `[15, 25)` overlaps `[10, 20)`, so it fails and uses no id. `[20, 30)` and `[0, 10)` only touch existing bookings at their endpoints, so both succeed. Room 2 was never added. The bookings come back sorted by start time even though `[0, 10)` was booked last.
Example 2:
```text
addRoom(3) -> True
book(3, 100, 200) -> 1
isAvailable(3, 150, 250) -> False
cancel(1) -> True
cancel(1) -> False
isAvailable(3, 150, 250) -> True
book(3, 50, 120) -> 2
getBookings(3) -> [[50, 120]]
isAvailable(4, 0, 10) -> False
```
`[150, 250)` overlaps `[100, 200)` until booking 1 is cancelled. A second cancel of the same id finds nothing. The next booking gets id 2 because ids are never reused, and room 4 does not exist.
```hint Which bookings can conflict
The bookings already in a room never overlap one another. If you keep them in order, how many of them can a new interval possibly collide with?
```
```hint Check and insert under concurrency
Two threads can both see a slot as free and then both insert. Decide which steps must happen as one atomic unit, and include the booking id counter in that decision.
```
### Constraints & Assumptions
- `1 <= roomId <= 10^4`
- `0 <= start < end <= 10^9`
- At most `10^5` calls are made across all methods.
- All operations must be thread-safe, with no double bookings under concurrent access.
### Clarifying Questions to Ask
- Must `isAvailable` and `getBookings` see a consistent view, or is an answer that is slightly stale under concurrent writes acceptable?
- Is in-memory state in a single process enough, or must bookings survive a restart?
- Is traffic spread across many rooms, or concentrated on a few popular ones? This decides whether one global lock is good enough.
- What should `book` and `isAvailable` do with an interval where `start >= end`, even though the constraints rule it out?
### What a Strong Answer Covers
- The data kept per room and per booking id, and how `cancel` finds a booking from its id alone.
- The overlap test for half-open intervals, including back-to-back bookings, and why only the neighboring bookings need to be checked.
- How the availability check, the insert and the id assignment stay atomic under concurrent calls.
- The cost of each operation for the sorted structure chosen, stated honestly.
- Behavior for unknown rooms and for unknown or already cancelled booking ids.
### Follow-up Questions
- How would you let bookings on different rooms run in parallel without allowing a double booking on the same room?
- How would you add a method that books the first room that is free for `[start, end)`?
- What changes if bookings must survive restarts or be served by several application servers?
Overview: Design an in-memory meeting room booking system that adds rooms, books and cancels half-open time slots, and answers availability queries without ever double booking a room. It tests interval overlap checks on sorted data, booking id tracking, and keeping check-and-insert atomic under concurrent calls.