The Exam Room interview question asks you to design a class that manages seating in a long row of N seats (numbered 0 to N-1). When a student enters, they must sit in the seat that maximizes their distance from the closest person already sitting. If there are multiple such seats, they sit in the one with the lowest number. When a student leaves, their seat becomes empty. You need to implement the seat() and leave(p) functions efficiently.
Companies like Apple and Google use this Design interview pattern to test your ability to manage dynamic intervals. It’s a classic optimization problem that requires balancing multiple constraints (maximizing distance, breaking ties with the smallest index). It evaluates whether you can use sophisticated data structures like a Heap or an Ordered Set to maintain sorted intervals and find the optimal insertion point in logarithmic time.
This problem is best solved using an Ordered Set or a Max-Heap of Intervals.
[0, 9].[a, b] that has the largest "middle point" distance. Also consider the boundaries (distance from 0 or N-1).p and merge the two adjacent intervals into one.
Using a self-balancing BST or an ordered set helps keep the occupied seats in order, allowing you to find neighbors in O(log S) time.Row size N=10.
seat(): First student sits at 0. (Always pick smallest index if empty).seat(): Second student sits at 9. (Max distance from 0).seat(): Third student sits in the middle of [0, 9], which is index 4. Distance is 4 from 0 and 5 from 9. (Rounded down).leave(4): Seat 4 is now empty. Intervals [0, 4] and [4, 9] merge back to [0, 9].seat() call, which is too slow for large N.(b - a) / 2.Practice problems involving "dynamic gaps" or "interval management." This problem is conceptually similar to managing memory blocks or scheduling tasks in a free-time window.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Design a Number Container System | Medium | Solve | |
| Design Task Manager | Medium | Solve | |
| Smallest Number in Infinite Set | Medium | Solve | |
| Sequentially Ordinal Rank Tracker | Hard | Solve | |
| Seat Reservation Manager | Medium | Solve |