Seat Reservation Manager
MediumUpdated: Jul 27, 2025
Practice on:
Problem
Design a system that manages the reservation state of n seats that are numbered from 1 to n.
Implement the SeatManager class:
SeatManager(int n)Initializes aSeatManagerobject that will managenseats numbered from1ton. All seats are initially available.int reserve()Fetches the smallest-numbered unreserved seat, reserves it, and returns its number.void unreserve(int seatNumber)Unreserves the seat with the givenseatNumber.
Examples
Example 1:
**Input**
["SeatManager", "reserve", "reserve", "unreserve", "reserve", "reserve", "reserve", "reserve", "unreserve"]
[[5], [], [], [2], [], [], [], [], [5]]
**Output**
[null, 1, 2, null, 2, 3, 4, 5, null]
**Explanation**
SeatManager seatManager = new SeatManager(5); // Initializes a SeatManager with 5 seats.
seatManager.reserve(); // All seats are available, so return the lowest numbered seat, which is 1.
seatManager.reserve(); // The available seats are [2,3,4,5], so return the lowest of them, which is 2.
seatManager.unreserve(2); // Unreserve seat 2, so now the available seats are [2,3,4,5].
seatManager.reserve(); // The available seats are [2,3,4,5], so return the lowest of them, which is 2.
seatManager.reserve(); // The available seats are [3,4,5], so return the lowest of them, which is 3.
seatManager.reserve(); // The available seats are [4,5], so return the lowest of them, which is 4.
seatManager.reserve(); // The only available seat is seat 5, so return 5.
seatManager.unreserve(5); // Unreserve seat 5, so now the available seats are [5].
Solution
Method 1 – MinHeap with Lazy Initialization
Intuition
To efficiently manage seat reservations, we use a min-heap to keep track of seats that have been unreserved. Instead of initializing the heap with all seat numbers, we maintain a counter for the next available seat. This way, we only add seats to the heap when they are unreserved, saving both time and space.
Approach
- Use a min-heap to store seat numbers that have been unreserved.
- Maintain a counter to track the next seat to reserve.
- On reserve, return the smallest seat number from the heap if available; otherwise, use the counter and increment it.
- On unreserve, add the seat number back to the heap.
Complexity
- ⏰ Time complexity:
O(log n)for reserve and unreserve due to heap operations. - 🧺 Space complexity:
O(n), for storing unreserved seat numbers in the heap.
Code
C++
#include <queue>
class SeatManager {
priority_queue<int, vector<int>, greater<int>> pq;
int next;
public:
SeatManager(int n) : next(1) {}
int reserve() {
if (!pq.empty()) {
int seat = pq.top(); pq.pop();
return seat;
}
return next++;
}
void unreserve(int seatNumber) {
pq.push(seatNumber);
}
};
Java
import java.util.PriorityQueue;
class SeatManager {
private final PriorityQueue<Integer> pq = new PriorityQueue<>();
private int next = 1;
public SeatManager(int n) {}
public int reserve() {
if (!pq.isEmpty()) {
return pq.poll();
}
return next++;
}
public void unreserve(int seatNumber) {
pq.offer(seatNumber);
}
}
Python
import heapq
class SeatManager:
def __init__(self, n: int) -> None:
self.pq: list[int] = []
self.next: int = 1
def reserve(self) -> int:
if self.pq:
return heapq.heappop(self.pq)
ans = self.next
self.next += 1
return ans
def unreserve(self, seatNumber: int) -> None:
heapq.heappush(self.pq, seatNumber)