There is an exam room with n seats in a single row labeled from 0 to n - 1.

When a student enters the room, they must sit in the seat that maximizes the distance to the closest person. If there are multiple such seats, they sit in the seat with the lowest number. If no one is in the room, then the student sits at seat number 0.

Design a class that simulates the mentioned exam room.

Implement the ExamRoom class:

  • ExamRoom(int n) Initializes the object of the exam room with the number of the seats n.
  • int seat() Returns the label of the seat at which the next student will sit.
  • void leave(int p) Indicates that the student sitting at seat p will leave the room. It is guaranteed that there will be a student sitting at seat p.

Input & Output

Example 1 — Basic Operations
$ Input: ExamRoom(10), seat(), seat(), seat(), seat(), leave(4), seat()
Output: [null,0,9,4,2,null,5]
💡 Note: Initialize room with 10 seats. First student sits at 0. Second at 9 (farthest from 0). Third at 4 (middle of 0-9). Fourth at 2 (middle of 0-4). Remove student at 4. Next student sits at 5 (middle of 2-9).
Example 2 — Small Room
$ Input: ExamRoom(3), seat(), seat(), seat()
Output: [null,0,2,1]
💡 Note: Room has 3 seats. First at 0, second at 2 (end), third at 1 (middle).
Example 3 — Leave and Reseat
$ Input: ExamRoom(5), seat(), seat(), leave(0), seat()
Output: [null,0,4,null,0]
💡 Note: First at 0, second at 4. Remove 0. Next student sits at 0 again (farthest from 4).

Constraints

  • 1 ≤ n ≤ 109
  • It is guaranteed that there will be a student sitting at seat p when leave(p) is called.
  • At most 104 calls will be made to seat and leave.

Visualization

Tap to expand
Exam Room - Ordered Set with Intervals INPUT ExamRoom(10) - 10 seats 0 1 2 3 4 5 6 7 8 9 Operations: 1. seat() --> 0 2. seat() --> 9 3. seat() --> 4 4. seat() --> 2 5. leave(4) 6. seat() --> 5 Input Values n = 10 (seats 0-9) ALGORITHM STEPS 1 Track Intervals Store gaps between seated students in ordered set 2 Find Max Distance For seat(): find interval with maximum midpoint dist 3 Handle Edges Seat 0 and n-1 are special (full distance, not half) 4 Update Intervals Split interval on seat() Merge intervals on leave() Interval Example After ops 1-3: 0 4 9 After leave(4), seat(): 0 2 5 9 FINAL RESULT Final Seat Configuration 0 1 2 3 4 5 6 7 8 9 Seated (previous) Last seated Output Array [null,0,9,4,2,null,5] Step-by-Step Results ExamRoom(10) --> null seat() --> 0 (empty room) seat() --> 9 (max from 0) seat() --> 4 (mid of 0-9) seat() --> 2 (mid of 0-4) leave(4) --> null seat() --> 5 (mid of 2-9) Key Insight: Use an ordered set to track occupied seats. For each seat() call, compute the maximum distance by checking intervals between adjacent seated students. Edge seats (0 and n-1) use full distance; middle seats use half. Time: O(N) for seat(), O(log N) for leave(). Space: O(N) for storing occupied seats. TutorialsPoint - Exam Room | Ordered Set with Intervals Approach
Asked in
Google 15 Facebook 8
67.0K Views
Medium Frequency
~25 min Avg. Time
892 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