KatInterview

Design an In-Memory Reservation Manager

Data Structure DesignAuto-graded · 13 test cases
MediumAuto-gradedData Structure Designhash-tabledesignstacks

Last updated

Implement an in-memory venue reservation system supporting quick seat allocation, cancellation tracking, and capacity queries.

Design an in-memory venue management component that handles real-time seat assignments and reservations across different event halls.

You need to process a sequence of operational commands given by operations along with their corresponding parameters in arguments.

The system should process the following commands:

  1. add_hall: Parameters [hall_id, capacity] (where capacity is given as a string representation of an integer). Registers a new hall with the specified seat capacity. Returns None.
  2. book_seat: Parameters [hall_id, user_id]. Reserves an available seat in the hall. Returns a string representing a unique reservation ID (e.g., "B1", "B2", etc.) if successful. Returns None if the hall does not exist or has no remaining available seats.
  3. cancel_booking: Parameters [booking_id]. Revokes the active reservation. Returns True if the booking was active and successfully cancelled, or False if the reservation ID is invalid or was already cancelled.
  4. get_hall_status: Parameters [hall_id]. Returns the count of currently available seats in the specified hall as an integer, or None if the hall does not exist.
  5. Unknown command: Returns None.

Rules & Operational Logic

  • Unique booking IDs are formatted as "B" followed by an incrementing integer counter starting at 1 ("B1", "B2", "B3", ...). The counter only increments on successful bookings.
  • Once assigned, counter values are never re-issued or reused, even after a booking is cancelled.
  • Seats assigned upon booking must be tracked internally such that allocations and cancellations run in $O(1)$ constant time per operation.
  • When a seat is cancelled, its internal seat slot becomes available for future bookings in that hall.

Constraints

  • $1 \le \text{len(operations)} \le 10^5$
  • operations and arguments have equal length.
  • hall_id, user_id, and booking_id strings consist of alphanumeric characters and underscores.
  • Total capacity across all registered halls will fit within memory constraints.

Examples

Example 1

Input: booking_system(['add_hall', 'book_seat', 'book_seat', 'book_seat', 'cancel_booking', 'get_hall_status'], [['Hall_A', '2'], ['Hall_A', 'User_1'], ['Hall_A', 'User_2'], ['Hall_A', 'User_3'], ['B1'], ['Hall_A']])

Output: [None, 'B1', 'B2', None, True, 1]

Hints

Hint 1

Use hash maps to map hall IDs to their status and booking IDs to their reservation metadata.

Hint 2

Maintain a stack or list of available seat indices per hall so seat allocation and release take O(1) time.

Hint 3

Ensure the global booking counter increments only when a seat is successfully reserved.