Imagine you're designing a system for a university exam room with n seats arranged in a single row, numbered from 0 to n-1. Students want to maintain maximum social distance while taking their exam!

When a student enters the room, they must sit in the seat that maximizes the distance to the closest occupied seat. If there are multiple seats with the same maximum distance, they choose the seat with the lowest number. If the room is empty, they sit at seat 0.

Your task is to implement an ExamRoom class that simulates this behavior:

  • ExamRoom(int n): Initialize the exam room with n seats
  • int seat(): Return the seat number where the next student sits
  • void leave(int p): Remove the student from seat p

Goal: Efficiently manage seat assignments to maximize distances between students.

Input & Output

basic_operations.py — Basic Seat Assignment
$ Input: ExamRoom(10) seat() → 0 seat() → 9 seat() → 4 leave(4) seat() → 4
Output: [null, 0, 9, 4, null, 4]
💡 Note: First student sits at 0 (empty room). Second student maximizes distance by sitting at 9. Third student sits at position 4 (middle of 0-9 gap). After student leaves position 4, next student again chooses position 4 as optimal.
tie_breaking.py — Lowest Index Preference
$ Input: ExamRoom(4) seat() → 0 seat() → 3 seat() → 1 leave(1) seat() → 1
Output: [null, 0, 3, 1, null, 1]
💡 Note: When multiple seats have same maximum distance (positions 1 and 2 both have distance 1 from nearest occupied seat), we choose the lower indexed seat (position 1).
single_seat.py — Edge Case
$ Input: ExamRoom(1) seat() → 0 leave(0) seat() → 0
Output: [null, 0, null, 0]
💡 Note: With only one seat available, student must always sit at position 0.

Visualization

Tap to expand
Exam Room SimulationS1S2S3Position: 0Position: n/2Position: n-1Distance = n/2Distance = n/2Strategy: Always choose position that maximizes distance to nearest studentKey Algorithm Steps:1. Maintain sorted list of occupied positions2. Find largest gap between consecutive occupied seats3. Place new student at gap midpoint or boundary4. Handle ties by choosing lower-indexed position
Understanding the Visualization
1
First Student
Empty room → sits at position 0 by default
2
Second Student
Maximizes distance → sits at far end (position n-1)
3
Third Student
Finds largest gap → sits in the middle of biggest empty section
4
Student Leaves
Creates new gap → recalculates optimal positions
5
Next Student
Chooses position with maximum distance to nearest occupied seat
Key Takeaway
🎯 Key Insight: The optimal seat is always at gap boundaries (positions 0 or n-1) or at the midpoint of the largest gap between consecutive occupied seats. This insight allows us to solve the problem efficiently in O(n) time per operation instead of checking every position.

Time & Space Complexity

Time Complexity
⏱️
O(n)

For each seat() operation, we scan through occupied seats once to find largest gap

n
2n
Linear Growth
Space Complexity
O(n)

Space for storing occupied seats in sorted order

n
2n
Linearithmic Space

Constraints

  • 1 ≤ n ≤ 109
  • At most 104 calls will be made to seat and leave
  • seat() is guaranteed to return a valid seat number
  • leave(p) is guaranteed to be called with an occupied seat p
Asked in
Google 42 Amazon 38 Meta 25 Microsoft 18
52.4K Views
Medium-High Frequency
~25 min Avg. Time
1.7K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen