Practice Exam Room
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Problem Statement
ExamRoom(n) manages seats 0 through n-1. seat() places a student as far as possible from the closest occupied seat, choosing the lowest index on a tie; the first student takes zero. leave(p) frees an occupied seat. Practice approach: Keep occupied seats sorted, comparing the left boundary, middle gaps, and right boundary. A middle gap chooses its lower midpoint. For faster operations, prioritize free intervals and support splitting and merging on insertion and departure. Boundary gaps use their full distance, unlike interior gaps. Test departures that join two gaps.
Source: Exam Room on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
["ExamRoom","seat","seat","seat","seat","leave","seat"] [[8],[],[],[],[],[3],[]]
[null,0,7,3,5,null,2]
After seats 0 and 7, the middle tie selects 3. Then 5 is farthest. Removing 3 makes 2 and 3 equally good, so 2 wins.
Constraints
Related LeetCode Problems
Practice more problems similar to Exam Room.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.