Movie Ticket Booking (BookMyShow)
medium · booking, atomicity, seat map
Brief-reported interview attribution (unverified): Flipkart, Amazon, MakeMyTrip
How to approach an LLD roundRequirements
- Functional: add a show with numbered seats, book seats all-or-nothing, cancel a booking (seats freed), list free seats and booking details.
- Non-functional: a seat must never be double-booked; the booking check and the hold must be one atomic step; the seat model must not hardcode a grid layout (it is a numbered list).
- Out of scope: pricing, payments, showtimes, seat selection UI.
Statement
Design a movie ticket booking system. A show has numbered seats
1..total_seats. Booking a set of seats succeeds only if all of them are free
(all-or-nothing); if any seat is taken or invalid, nothing is booked. Cancelling
frees the seats. (Payment is out of scope for this problem; booking confirms
immediately. In production the check-and-book must be atomic so two users cannot
take the same seat — in this single-threaded simulation the method itself is the
atomic unit.)
class BookMyShow:
def __init__(self):
def add_show(self, show_id, movie, screen, total_seats):
def available_seats(self, show_id):
# -> sorted list of free seat numbers ([] for an unknown show)
def book(self, show_id, user_id, seat_numbers):
# -> booking id (int, starting at 1), or None if any seat is taken/invalid,
# the same seat is listed twice, or the show is unknown or the seat list is empty
def cancel(self, booking_id):
# -> True if the booking was active and is now cancelled, False otherwise
def booking_details(self, booking_id):
# -> {"show":, "user":, "seats": [...], "active": bool}, or NoneRules. Seat numbers start at 1. Booking ids start at 1 and increase. A
cancelled booking's seats become free again; cancelling twice returns False.
booking_details returns the seats in the order they were booked. A request listing the same seat twice is rejected (returns None).
Tests use explicit inputs and injected clocks/callbacks. No network, real threads or application-server execution is involved.
Class design (SOLID)
Seat (number + which booking holds it; SRP). Show (movie, screen, seats; SRP — can_book also rejects a request that lists the same seat twice). Booking (show, user, seats, active flag; SRP). BookMyShow orchestrates. No pattern is forced; the decomposition (seat vs show vs booking) is the point.
Public demo
# Public demo: run after implementing the class API above.
b = BookMyShow()
b.add_show('S1', 'Dune', 'Screen 1', 5)
print(b.available_seats('S1'))Reference solution and hidden tests
Locked. Open the interactive editor and attempt the problem; the reference, hidden test descriptions, pattern explanation and complexity are available only after an explicit reveal.
Concurrency
Two users booking the same seat at once is the classic race: the free-check and the hold must be atomic — one lock per show around book/cancel. In production, this is the row lock on the show. The reference is single-threaded; the lock is described here.
Trade-offs & extensions
All-or-nothing booking is simple but a 10-seat request can fail because one seat is taken; a partial-booking mode would need a different book contract. Extending: a PricingStrategy (seat price by row) and a hold-with-expiry (a Booking that times out).
Starter code
class Seat:
def __init__(self, seat_no):
"""Initialize the state described in the problem."""
pass
def is_free(self):
"""Implement the return value and rules in the API contract."""
pass
class Show:
def __init__(self, show_id, movie, screen, total_seats):
"""Initialize the state described in the problem."""
pass
def free_seats(self):
"""Implement the return value and rules in the API contract."""
pass
def can_book(self, seat_numbers):
"""Implement the return value and rules in the API contract."""
pass
def hold(self, seat_numbers, booking_id):
"""Implement the return value and rules in the API contract."""
pass
def release(self, seat_numbers):
"""Implement the return value and rules in the API contract."""
pass
class Booking:
def __init__(self, booking_id, show_id, user, seats):
"""Initialize the state described in the problem."""
pass
def details(self):
"""Implement the return value and rules in the API contract."""
pass
class BookMyShow:
def __init__(self):
"""Initialize the state described in the problem."""
pass
def add_show(self, show_id, movie, screen, total_seats):
"""Implement the return value and rules in the API contract."""
pass
def available_seats(self, show_id):
"""-> sorted list of free seat numbers ([] for an unknown show)"""
pass
def book(self, show_id, user_id, seat_numbers):
"""-> booking id (int, starting at 1), or None if any seat is taken/invalid, the same seat is listed twice, or the show is unknown or the seat list is empty"""
pass
def cancel(self, booking_id):
"""-> True if the booking was active and is now cancelled, False otherwise"""
pass
def booking_details(self, booking_id):
"""-> {"show":, "user":, "seats": [...], "active": bool}, or None"""
pass
Each test has a five-second execution limit. Your practice drafts and solved progress stay in this browser.
Return to the Python interpreter