Exam Room
MediumUpdated: Aug 2, 2025
Practice on:
Problem
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 seatsn.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 seatpwill leave the room. It is guaranteed that there will be a student sitting at seatp.
Examples
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 <= 10^9- It is guaranteed that there is a student sitting at seat
p. - At most
104calls will be made toseatandleave.
Solution
Method 1 – Ordered Set and Interval Greedy
Intuition
To maximize the distance to the closest person, always choose the largest empty interval. Use an ordered set to keep track of occupied seats and compute the best seat each time. For leaving, simply remove the seat from the set.
Approach
- Use a sorted set to store occupied seats.
- For
seat(), check the distance from 0 to the first seat, between all pairs of occupied seats, and from the last seat to n-1. Choose the seat that maximizes the minimum distance. - For
leave(p), removepfrom the set.
Code
C++
class ExamRoom {
set<int> seats;
int n;
public:
ExamRoom(int n): n(n) {}
int seat() {
if (seats.empty()) {
seats.insert(0);
return 0;
}
int prev = -1, max_dist = 0, ans = 0;
for (int s : seats) {
if (prev == -1) {
if (s > max_dist) {
max_dist = s;
ans = 0;
}
} else {
int d = (s - prev) / 2;
if (d > max_dist) {
max_dist = d;
ans = prev + d;
}
}
prev = s;
}
if (n - 1 - *seats.rbegin() > max_dist) {
ans = n - 1;
}
seats.insert(ans);
return ans;
}
void leave(int p) {
seats.erase(p);
}
};
Java
import java.util.TreeSet;
class ExamRoom {
TreeSet<Integer> seats = new TreeSet<>();
int n;
public ExamRoom(int n) { this.n = n; }
public int seat() {
if (seats.isEmpty()) {
seats.add(0);
return 0;
}
int prev = -1, maxDist = 0, ans = 0;
for (int s : seats) {
if (prev == -1) {
if (s > maxDist) {
maxDist = s;
ans = 0;
}
} else {
int d = (s - prev) / 2;
if (d > maxDist) {
maxDist = d;
ans = prev + d;
}
}
prev = s;
}
if (n - 1 - seats.last() > maxDist) {
ans = n - 1;
}
seats.add(ans);
return ans;
}
public void leave(int p) {
seats.remove(p);
}
}
Python
class ExamRoom:
def __init__(self, n: int):
self.n = n
self.seats = set()
def seat(self) -> int:
if not self.seats:
self.seats.add(0)
return 0
arr = sorted(self.seats)
max_dist = arr[0]
ans = 0
for i in range(1, len(arr)):
d = (arr[i] - arr[i-1]) // 2
if d > max_dist:
max_dist = d
ans = arr[i-1] + d
if self.n - 1 - arr[-1] > max_dist:
ans = self.n - 1
self.seats.add(ans)
return ans
def leave(self, p: int) -> None:
self.seats.remove(p)
Rust
use std::collections::BTreeSet;
struct ExamRoom {
n: i32,
seats: BTreeSet<i32>,
}
impl ExamRoom {
fn new(n: i32) -> Self {
Self { n, seats: BTreeSet::new() }
}
fn seat(&mut self) -> i32 {
if self.seats.is_empty() {
self.seats.insert(0);
return 0;
}
let mut prev = -1;
let mut max_dist = 0;
let mut ans = 0;
for &s in &self.seats {
if prev == -1 {
if s > max_dist {
max_dist = s;
ans = 0;
}
} else {
let d = (s - prev) / 2;
if d > max_dist {
max_dist = d;
ans = prev + d;
}
}
prev = s;
}
if self.n - 1 - *self.seats.iter().rev().next().unwrap() > max_dist {
ans = self.n - 1;
}
self.seats.insert(ans);
ans
}
fn leave(&mut self, p: i32) {
self.seats.remove(&p);
}
}
TypeScript
class ExamRoom {
private n: number;
private seats: Set<number>;
constructor(n: number) {
this.n = n;
this.seats = new Set();
}
seat(): number {
if (this.seats.size === 0) {
this.seats.add(0);
return 0;
}
const arr = Array.from(this.seats).sort((a, b) => a - b);
let maxDist = arr[0], ans = 0;
for (let i = 1; i < arr.length; ++i) {
const d = Math.floor((arr[i] - arr[i-1]) / 2);
if (d > maxDist) {
maxDist = d;
ans = arr[i-1] + d;
}
}
if (this.n - 1 - arr[arr.length-1] > maxDist) {
ans = this.n - 1;
}
this.seats.add(ans);
return ans;
}
leave(p: number): void {
this.seats.delete(p);
}
}
Complexity
- ⏰ Time complexity:
O(log n)per seat/leave operation (for ordered set operations). - 🧺 Space complexity:
O(n)for storing occupied seats.