Examination Hall

There is an exam room with n seats in a single row, numbered 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 the room is empty, the first student always sits at seat 0.

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 index of the seat the new student will take.
  • 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.

Example 1:

Input: ["ExamRoom", "seat", "seat", "seat", "seat", "leave", "seat"]

[[10], [], [], [], [], [4], []]

Output: [null, 0, 9, 4, 2, null, 5]

Explanation: In a room of 10 seats (0 to 9), the first student always takes seat 0. The second student maximizes distance by taking the opposite end, seat 9. The third student sits exactly in the middle at seat 4. For the fourth student, the gaps are between 0-4 and 4-9. The midpoint of 0-4 is 2 (distance 2), and the midpoint of 4-9 is 6 (distance 2). Because there is a tie in maximum distance, the student is assigned the lowest-numbered seat, which is 2. We then have the student at seat 4 leave. The largest gap is now between 2 and 9, so the final student sits at the midpoint, seat 5.

Example 2:

Input: ["ExamRoom", "seat", "seat", "seat", "leave", "leave", "seat"]

[[4], [], [], [], [0], [3], []]

Output: [null, 0, 3, 1, null, null, 3]

Explanation: In a small 4-seat room (0 to 3), the first two students take the extreme edges, 0 and 3. The third student has a choice between seat 1 or seat 2; both have a maximum distance of 1 to the closest person. The tie-breaker rule assigns them to the lower index, seat 1. Next, both students at the extreme edges (0 and 3) leave the room, leaving only the student at seat 1. When the final student enters, the furthest they can sit from seat 1 is all the way at the right edge, so they take seat 3.

Now Your Turn!

Pick the correct output for the given input

Input: ["ExamRoom", "seat", "seat", "leave", "seat", "seat", "leave", "seat"]

[[7], [], [], [0], [], [], [6], []]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= n <= 109
  • It is guaranteed that there is a student sitting at seat p.
  • At most 104 calls will be made to seat and leave.

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class ExamRoom {
public:
ExamRoom(int n) {
}
int seat() {
}
void leave(int p) {
}
};
Test Case

Input:

Methods
Params