AlgoMaster Logo
AlgoMasterDesign Ticket Booking Systemmedium

Design Ticket Booking System

medium

Design a thread-safe ticket booking system for a venue whose seats are numbered from 0 to totalSeats - 1.

Implement the following operations:

  • book(seat) reserves a specific seat and returns true only when that seat was available.
  • bookAny() reserves and returns any available seat, or returns -1 when the venue is full.
  • cancel(seat) releases a seat and returns true only when that seat was booked.
  • isBooked(seat) returns whether a specific seat is currently booked.
  • availableSeats() returns the number of seats that are currently available.

Every operation must be thread-safe and linearizable. A seat must never be returned successfully to two customers at the same time, and the available-seat count must always agree with the booking state.

The judge creates all customer threads. Your class should synchronize access to its state rather than create worker threads itself.

The judge also preloads the standard concurrency and collection APIs for every supported language. You do not need to add import, include, using, or package statements.

Example 1:

Input:

Output:

Example 2:

Input:

Output:

Explanation: The first two results may appear in either order because bookAny() may choose any available seat.

Constraints

  • 1 <= totalSeats <= 10_000
  • 0 <= seat < totalSeats
  • The judge calls book, cancel, and isBooked only with valid seat numbers.
  • At most 100_000 operations are performed.
  • Every operation may be called concurrently.
  • bookAny() may return any available seat; no particular selection order is required.
Loading...

Input

TicketBookingSystem(4)
book(2)
book(2)
isBooked(2)
availableSeats()
cancel(2)
availableSeats()

Output

[true, false, true, 3, true, 4]

Run is a quick check against the first couple of scenarios, which is roughly what these examples describe. Submit puts your class under the full set, which stays hidden.