Design a restaurant table booking system that assigns the smallest fitting table
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a restaurant table booking system that manages table reservations for different party sizes and time slots.
Implement a `RestaurantBooking` class with these methods:
- `RestaurantBooking()` initializes an empty booking system.
- `addTable(tableId: string, capacity: int) -> boolean` adds a table with the given capacity. It returns `false` if the table ID already exists or the capacity is invalid.
- `bookTable(bookingId: string, partySize: int, time: string) -> string` books a table for a party at a specific time. Check these cases in this order:
- If the booking ID already exists, return `"Booking ID already exists"`.
- If the party size is invalid, return `"Invalid party size"`.
- If no suitable table is free at that time, return `"No available table"`.
- Otherwise, assign the smallest fitting table and return its table ID.
- `cancelBooking(bookingId: string) -> boolean` cancels a booking. It returns `false` if the booking is not found.
- `getAvailableTables(time: string, partySize: int) -> string[]` returns the tables that are free at that time and fit the party size, sorted by capacity, smallest first.
- `getBooking(bookingId: string) -> [tableId, partySize, time] | null` returns the booking details, or `null` if the booking is not found.
Table assignment rule: when several free tables can fit a party, assign the one with the smallest capacity, which keeps larger tables open for larger parties. If capacities are equal, take the table ID that comes first alphabetically. `getAvailableTables` lists tables in the same order.
In the examples, the first input line lists the calls and the second their arguments. The output lists each call's return value, with `null` for the constructor.
Example 1:
```text
Input:
["RestaurantBooking", "addTable", "addTable", "addTable", "bookTable", "bookTable", "getBooking", "cancelBooking", "getAvailableTables"]
[[], ["T1", 2], ["T2", 4], ["T3", 6], ["B1", 3, "18:00"], ["B2", 2, "18:00"], ["B1"], ["B1"], ["18:00", 2]]
Output:
[null, true, true, true, "T2", "T1", ["T2", 3, "18:00"], true, ["T2", "T3"]]
```
B1 needs 3 seats, so T1 (2 seats) is too small and T2 (4 seats) is the smallest table that fits. B2 needs 2 seats and T1 fits it exactly, so B2 gets T1. Cancelling B1 frees T2 at 18:00, but B2 still holds T1, so a party of 2 at 18:00 can use T2 or T3.
Example 2:
```text
Input:
["RestaurantBooking", "addTable", "addTable", "addTable", "addTable", "bookTable", "bookTable", "bookTable", "bookTable", "bookTable", "bookTable", "bookTable", "cancelBooking", "getBooking"]
[[], ["Window", 4], ["Booth", 4], ["Booth", 8], ["Bar", 0], ["X1", 4, "19:00"], ["X1", 2, "20:00"], ["X2", 0, "19:00"], ["X3", 5, "19:00"], ["X4", 3, "19:00"], ["X5", 1, "19:00"], ["X5", 1, "19:30"], ["X9"], ["X3"]]
Output:
[null, true, true, false, false, "Booth", "Booking ID already exists", "Invalid party size", "No available table", "Window", "No available table", "Booth", false, null]
```
The second "Booth" is a duplicate table ID and a capacity of 0 is invalid. Booth and Window both seat 4, so X1 gets Booth, which comes first alphabetically. No table seats 5, so X3 fails. After X4 takes Window, both tables are busy at 19:00 and X5 fails there. A failed call stores nothing, so X5 can still be booked at 19:30, where both tables are free and Booth wins the tie again. X3 never became a booking, so `getBooking("X3")` returns `null`.
```hint Which lookups you need
List the questions each method asks of the data: find a booking by ID, and decide whether a given table is taken at a given time. Pick structures that answer the second one without scanning every booking.
```
```hint Keep the methods consistent
bookTable and getAvailableTables use the same filter and the same order, so write that logic once. Then check that cancelBooking undoes everything bookTable wrote.
```
### Constraints & Assumptions
- `1 <= tableId.length, bookingId.length <= 50`
- Capacities and party sizes are at most 100. A value below 1 can be passed and is invalid.
- Time is a string in `"HH:MM"` format. A booking holds its table for that exact slot only, so bookings at `"18:00"` and `"18:30"` never conflict.
- Table IDs compare as plain strings for the tie-break, so `"T10"` sorts before `"T9"`.
- A failed `bookTable` call changes nothing. A cancelled booking is removed, and its ID may be booked again.
- At most 10,000 calls are made in total.
### Clarifying Questions to Ask
- Does a booking hold the table for a fixed duration, so nearby times conflict, or only for its exact time slot?
- After a booking is cancelled, can its booking ID be used again?
- Can a large party be split across tables or seated at joined tables?
- Can tables be removed or resized while they hold bookings?
### What a Strong Answer Covers
- A data model that answers "is this table free at this time?" in constant time and stays consistent through booking and cancelling.
- The capacity-then-ID order, applied the same way to table assignment and to the list of available tables.
- The exact validation order and return values, with no state written on a failed call.
- The cost of each method in terms of the number of tables, and whether a linear scan is fast enough for 10,000 calls.
- Edge cases such as duplicate table IDs, cancelling an unknown booking, and booking a cancelled ID again.
### Follow-up Questions
- How would the design change if every booking lasts 90 minutes, so a booking at 18:00 also blocks 19:00?
- With thousands of tables, how would you find the smallest free table that fits without scanning all of them?
- How would you add a waitlist that offers a freed table to the next waiting party that fits?
- Two hosts book the last 4-seat table at 19:00 from different devices at the same moment. How do you prevent a double booking?
Overview: Implement a restaurant reservation class that adds tables, books the smallest free table that fits a party at a given time slot, cancels bookings and lists open tables in capacity order. It tests data structure choice, tie-breaking rules, validation order and keeping state consistent when a booking is cancelled.