Design a restaurant waitlist system
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates a candidate's ability to design efficient data structures and algorithms for managing an ordered waitlist with dynamic additions, cancellations, and capacity-based selections, emphasizing performance under high operation counts.
Constraints
- 0 <= len(operations) <= 2 * 10^5
- For ('addParty', name, size), 1 <= size <= 20
- For ('seatTable', capacity), 1 <= capacity <= 20
- partyId values are assigned in increasing order starting from 1
- Aim for better-than-linear time per operation
Examples
Input: [('addParty', 'A', 4), ('addParty', 'B', 2), ('seatTable', 2), ('seatTable', 4)]
Expected Output: [1, 2, 2, 1]
Explanation: Party ids start at 1. B is seated first because A does not fit a table of capacity 2, then A is seated by the next table.
Input: [('addParty', 'A', 4), ('addParty', 'B', 2), ('addParty', 'C', 3), ('getPosition', 3), ('seatTable', 2), ('getPosition', 3), ('cancelParty', 1), ('getPosition', 3), ('cancelParty', 1), ('seatTable', 2), ('seatTable', 3), ('getPosition', 3)]
Expected Output: [1, 2, 3, 2, 2, 1, True, 0, False, None, 3, -1]
Explanation: C starts with two parties ahead. B is seated first by the 2-seat table, then A cancels, leaving C at position 0. A second cancel on A fails, a 2-seat table fits nobody, then a 3-seat table seats C.
Input: [('addParty', 'A', 4), ('addParty', 'B', 5), ('addParty', 'C', 2), ('seatTable', 3), ('getPosition', 2), ('seatTable', 4), ('seatTable', 4), ('cancelParty', 2), ('seatTable', 5)]
Expected Output: [1, 2, 3, 3, 1, 1, None, True, None]
Explanation: For capacity 3, C is the earliest party that fits. Then B has one party ahead. A is later seated by a 4-seat table, another 4-seat table fits nobody, B cancels, and the final 5-seat table finds no one waiting.
Input: []
Expected Output: []
Explanation: No operations means no returned results.
Input: [('addParty', 'Solo', 1), ('getPosition', 1), ('seatTable', 1), ('getPosition', 1), ('cancelParty', 1)]
Expected Output: [1, 0, 1, -1, False]
Explanation: The only party starts at position 0, gets seated, is no longer on the waitlist, and therefore cannot be cancelled afterward.
Hints
- Because party size is only 1..20, you can keep separate candidate structures for each size and check only a constant number of buckets when seating a table.
- To answer getPosition after many cancellations and seatings, maintain active parties by arrival index in a Fenwick tree (Binary Indexed Tree) or another order-statistics structure.