Design a restaurant waitlist system
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Overview: 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.
Read the full Google Software Engineer interview experience this question came from
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.
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.
Community answers
Answer by rupalsharmadel