Skip to content
Decoder

Seat Reservation Manager

Medium

Problem

A theatre has n seats numbered 1 to n, all free at the start. Build a class SeatManager:

- SeatManager(n) sets up the n seats. - reserve() books the smallest-numbered free seat and returns its number. - unreserve(seat_number) (seatNumber in TypeScript) frees that seat again.

Example 1

m = SeatManager(5)

Example 2

Input
m.reserve()
Output
1

Example 3

Input
m.reserve()
Output
2

Example 4

m.unreserve(2)

Example 5

Input
m.reserve()
Output
2

Example 6

Input
m.reserve()
Output
3

Constraints

  • 1 <= n <= 10^5
  • At most 10^5 calls are made in total.
  • reserve is only called when at least one seat is free.
  • unreserve is only called on a seat that is currently reserved.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.