Crash Code Part-6 (Event Reservation System!)
Trying to solve old coding questions, because reasons: The Theater Booking Service
1544 Words [Mind Tax: 7:01m]
20 August 2026, 6:30:00 PM
Problem Description
You are tasked with implementing a ShowBooking service for a theater with multiple auditoriums. Each auditorium has a fixed seating capacity and can host a single event per day.
The service needs to:
- Store which event will take place in an auditorium of the theater.
- Avoid conflicts when multiple customers are concurrently booking tickets for the same event by utilizing an “under booking” state.
You must implement two classes:
Repo(implementing theIRepointerface): Handles all state and storage—mapping events to auditoriums, tracking active “under booking” requests, and storing successfully confirmed bookings.BookingService(implementing theIBookingServiceinterface): Validates capacity constraints and processes booking transitions.
Constraints
- 1 <= totalNumberOfRequests <= 100,000
- 1 <= userId <= 100,000
- 1 <= eventId <= 100,000
- 1 <= auditoriumNumber <= 100,000
Input
- The first line contains an integer
n, the total number of auditoriums. - The second line contains
nspace-separated integers representing theeventIdassigned to each auditorium. (The indexiof the element is itsauditoriumNumber). - The third line contains
nspace-separated integers representing thecapacityof each auditorium. - The fourth line contains the number of incoming booking requests
q. - The next
qlines contain queries structured as: startBookingProcess <eventId> <userId>confirmBookingStatus <eventId> <userId> <true/false>
Output
- For
startBookingProcess, outputtrueif initiated successfully (capacity allowed), otherwisefalse. - For
confirmBookingStatus, outputsuccessful <auditoriumNumber>if the booking succeeded, orfailed <auditoriumNumber>if it did not. (Note: The driver code handles this printing based on your returned objects).
Examples
Example
Input:
6
4 5 7 1 2 3
2 2 2 2 2 2
5
startBookingProcess 7 1
startBookingProcess 7 2
startBookingProcess 7 3
confirmBookingStatus 7 1 true
startBookingProcess 7 4
Output:
true
true
false
successful 2
false
Explanation:
n = 6 auditoriums.
Event 7 is mapped to auditorium index 2, with a capacity of 2.
Query 1: User 1 starts booking Event 7. Capacity is 0/2. Result: true. (1 seat under booking).
Query 2: User 2 starts booking Event 7. Capacity is 1/2. Result: true. (2 seats under booking).
Query 3: User 3 starts booking Event 7. Capacity is 2/2. Result: false. (Rejected).
Query 4: User 1 confirms booking. User 1 moves from "under booking" to "successful". Output: successful 2.
Query 5: User 4 starts booking Event 7. Currently 1 successful + 1 under booking = 2/2 capacity. Result: false.
The Concepts Under the Hood
Instead of looping over complex lists of reservations every time a user makes a request, we leverage Python’s Dictionaries and Sets for O(1) time complexity lookups. This allows the application to handle massive inputs efficiently.
Here is the logical flow:
- Lightweight Data Classes: We use Python’s
NamedTupleto quickly defineAuditoriumandBookingConclusiondata structures. They are immutable and highly readable. - State Management via Dictionaries: Inside the
Repoclass, we maintain three dictionaries.events_auditoriummaps anevent_idto itsAuditoriumobject.under_bookingmaps anevent_idto aset()ofuser_ids who are in the middle of a transaction. (Sets natively prevent accidental duplicate bookings from the same user).successful_bookingmaps anevent_idto aset()ofuser_ids with finalized tickets.
- Safe Removals: When a booking is finalized or fails, we must remove the user from the
under_bookingset. Using Python’sset.discard()instead ofset.remove()is a great trick to avoid unexpectedKeyErrorcrashes in case of bad inputs or misaligned states.
Visualizing the Memory State
Let’s look at how the memory states look inside our Repo during the critical steps of the example test case.
Step 1: Initialization
After reading the inputs, Event 7 is registered in Auditorium 2 with a capacity of 2.
events_auditorium[7] = Auditorium(auditorium_number=2, event_id=7, capacity=2)
under_booking[7] = set()
successful_booking[7] = set()
Step 2: Processing startBookingProcess
Users 1 and 2 start booking. We check len(under_booking) + len(successful_booking). It’s 0 < 2, then 1 < 2. Both users are added to the set.
under_booking[7] = {1, 2}
When User 3 tries, 2 < 2 is false. They are rejected.
Step 3: Processing confirmBookingStatus
User 1 confirms successfully. We discard(1) from under_booking and add(1) to successful_booking.
under_booking[7] = {2}
successful_booking[7] = {1}
When User 4 tries to start a booking, we check lengths again: 1 (under) + 1 (successful) = 2. Since 2 < 2 is false, User 4 is instantly rejected!
Solve
Here is the fully combined, runnable code including the driver boilerplate and the original documentation strings. You can run this directly in your Python terminal.
| |