Skip to main content
Back to problems
#855
Medium Algorithms

Exam room

Design Heap (Priority Queue) Ordered Set
43.2% acceptance
Feb 22, 2026
1416
524
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 set. 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.

Solution

Rust
Time O(n log n)
Space O(n)
LeetCode
solution.rs
/*
 * 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 set.
 * 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
 * ExamRoom examRoom = new ExamRoom(10);
 * examRoom.seat(); // return 0, no one is in the room, then the student sits at seat number 0.
 * examRoom.seat(); // return 9, the student sits at the last seat number 9.
 * examRoom.seat(); // return 4, the student sits at the last seat number 4.
 * examRoom.seat(); // return 2, the student sits at the last seat number 2.
 * examRoom.leave(4);
 * examRoom.seat(); // return 5, the student sits at the last seat number 5.
 * 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.

 * struct ExamRoom {

 * }


 * /** 
 *  * `&self` means the method takes an immutable reference.
 *  * If you need a mutable reference, change it to `&mut self` instead.
 *  */
 * impl ExamRoom {

 *     fn new(n: i32) -> Self {

 *     }

 *     fn seat(&self) -> i32 {

 *     }

 *     fn leave(&self, p: i32) {

 *     }
 * }
 */

/**
 * Your ExamRoom object will be instantiated and called as such:
 * let obj = ExamRoom::new(n);
 * let ret_1: i32 = obj.seat();
 * obj.leave(p);
 */

use std::collections::BTreeSet;
struct ExamRoom {
  n: i32,
  seats: BTreeSet<i32>,
}
impl ExamRoom {
  fn new(n: i32) -> Self {
    ExamRoom { n, seats: BTreeSet::new() }
  }
  fn seat(&mut self) -> i32 {
    if self.seats.is_empty() {
      self.seats.insert(0);
      return 0;
    }
    let mut best_pos = 0;
    let mut best_dist = *self.seats.iter().next().unwrap(); // distance from 0
    let seats: Vec<i32> = self.seats.iter().cloned().collect();
    for w in seats.windows(2) {
      let d = (w[1] - w[0]) / 2;
      if d > best_dist {
        best_dist = d;
        best_pos = w[0] + d;
      }
    }
    // Check end
    let last = *self.seats.iter().next_back().unwrap();
    if self.n - 1 - last > best_dist {
      best_pos = self.n - 1;
    }
    self.seats.insert(best_pos);
    best_pos
  }
  fn leave(&mut self, p: i32) {
    self.seats.remove(&p);
  }
}