Seat Reservation Manager
MedPrompt
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.
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].
Constraints:
1 <= n <= 1051 <= seatNumber <= n- For each call to
reserve, it is guaranteed that there will be at least one unreserved seat. - For each call to
unreserve, it is guaranteed thatseatNumberwill be reserved. - At most
105calls in total will be made toreserveandunreserve.
Approaches
3 approaches with complexity analysis and trade-offs.
This approach uses a boolean array to keep track of the status of each seat. A true value at an index indicates the seat is reserved, while false indicates it's available.
Algorithm
SeatManager(n):- Create a boolean array
reservedof sizen+1.
- Create a boolean array
reserve():- Iterate
ifrom 1 ton. - If
reserved[i]isfalse:- Set
reserved[i]totrue. - Return
i.
- Set
- Iterate
unreserve(seatNumber):- Set
reserved[seatNumber]tofalse.
- Set
Walkthrough
Initialization
In the SeatManager(n) constructor, we create a boolean array seats of size n + 1 (to accommodate 1-based indexing). We initialize all entries to false, signifying that all seats from 1 to n are initially available.
reserve()
To reserve the smallest-numbered seat, we perform a linear scan through the seats array, starting from index 1. The first index i we find where seats[i] is false corresponds to the smallest available seat. We then mark this seat as reserved by setting seats[i] = true and return the seat number i.
unreserve(seatNumber)
To unreserve a seat, we simply access the corresponding index in the array and set its value back to false. This is a constant-time operation.
class SeatManager { private boolean[] reserved; private int n; public SeatManager(int n) { this.n = n; this.reserved = new boolean[n + 1]; // Using 1-based indexing } public int reserve() { for (int i = 1; i <= n; i++) { if (!reserved[i]) { reserved[i] = true; return i; } } return -1; // Should not happen based on problem constraints } public void unreserve(int seatNumber) { if (seatNumber >= 1 && seatNumber <= n) { reserved[seatNumber] = false; } }}Complexity
Time
- **Constructor**: O(n) to initialize the boolean array. - **`reserve()`**: O(n) because in the worst case, we might have to scan the entire array to find an available seat. - **`unreserve()`**: O(1) for direct array access.
Space
O(n) to store the state of `n` seats in the boolean array.
Trade-offs
Pros
Simple to understand and implement.
unreserveis very fast, taking O(1) time.
Cons
The
reserveoperation is slow, with a time complexity of O(n). This can lead to a 'Time Limit Exceeded' error on platforms with strict time limits, especially given that n can be up to 10^5.
Solutions
Solution
public class SeatManager { private SortedSet < int > availableSeats ; public SeatManager ( int n ) { availableSeats = new SortedSet < int >(); for ( int i = 1 ; i <= n ; i ++) { availableSeats . Add ( i ); } } public int Reserve () { int reservedSeat = availableSeats . Min ; availableSeats . Remove ( reservedSeat ); return reservedSeat ; } public void Unreserve ( int seatNumber ) { availableSeats . Add ( seatNumber ); } } /** * Your SeatManager object will be instantiated and called as such: * SeatManager obj = new SeatManager(n); * int param_1 = obj.Reserve(); * obj.Unreserve(seatNumber); */Video walkthrough
Newsletter
One sharp idea, every week
System design and interview prep — short enough to finish.
No spam. Unsubscribe anytime.
Practice
Same difficulty — related problems to reinforce the pattern.