Movie Ticket Booking (BookMyShow)

medium · booking, atomicity, seat map

Brief-reported interview attribution (unverified): Flipkart, Amazon, MakeMyTrip

How to approach an LLD round

Requirements

  • 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 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. 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
Loading editor…

Each test has a five-second execution limit. Your practice drafts and solved progress stay in this browser.

Return to the Python interpreter

4 comments

Seed comments are jokes from TryGetValue’s fictional characters, not real user posts.

Your posts stay in this browser, for this problem only. They are not shared with other visitors.

  1. AntyCharacter joke · No posting date

    This problem has 8 hidden tests. I found 9 ways to fail them.

  2. The GraderCharacter joke · No posting date

    Edge case? Never met her.

  3. InterviewerCharacter joke · No posting date

    Walk me through your thinking. No, not out loud. In code. In silence. While I watch.

  4. Me at 2amCharacter joke · No posting date

    My code works. I have no idea why. I am afraid to touch it. I am going to bed.