Movie Ticket Booking (BookMyShow)
medium · booking, atomicity, seat map
Brief-reported interview attribution (unverified): Flipkart, Amazon, MakeMyTrip
How to approach an LLD roundDesign 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
# 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 None
Rules. 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.
Tests use explicit inputs and injected clocks/callbacks. No network, real threads or application-server execution is involved.
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'))Starter code
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 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 compiler