Seat Reservation Manager - 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.
Input & Output
Example 1 — Basic Operations
$
Input:
operations = ["SeatManager", "reserve", "reserve", "unreserve", "reserve"], values = [[5], [], [], [2], []]
›
Output:
[null, 1, 2, null, 2]
💡 Note:
Initialize with 5 seats (1-5 available). reserve() returns 1 (smallest). reserve() returns 2. unreserve(2) makes seat 2 available. reserve() returns 2 (seat 2 is the smallest available since seat 1 is still reserved).
Example 2 — Multiple Unreserve
$
Input:
operations = ["SeatManager", "reserve", "reserve", "unreserve", "unreserve", "reserve"], values = [[3], [], [], [1], [2], []]
›
Output:
[null, 1, 2, null, null, 1]
💡 Note:
Initialize with 3 seats. Reserve seats 1 and 2. Unreserve both seats 1 and 2. Next reserve() returns 1 (smaller than 2).
Example 3 — Edge Case with Single Seat
$
Input:
operations = ["SeatManager", "reserve", "unreserve", "reserve"], values = [[1], [], [1], []]
›
Output:
[null, 1, null, 1]
💡 Note:
Only 1 seat available. Reserve it (returns 1), unreserve it, reserve again (returns 1).
Constraints
- 1 ≤ n ≤ 105
- 1 ≤ seatNumber ≤ n
- At most 105 calls will be made to reserve and unreserve
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code