Design an In-Memory Movie Ticket Booking System with Show Scheduling and Atomic Seat Booking

Quick Overview

Design an in-memory movie ticket booking system that models movies, theatres, screens, shows and bookings behind one facade, with exact return rules for every method. It tests class modeling, non-overlapping show scheduling, all-or-nothing seat booking, and cancellation that keeps booking history intact.

Design an In-Memory Movie Ticket Booking System with Show Scheduling and Atomic Seat Booking

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design an in-memory movie ticket booking system that models movies, theatres, screens, shows, seats and bookings behind a single public facade. A movie can have many shows. A theatre belongs to one city and has many screens. Each screen has a fixed `rows x cols` layout whose seats are numbered `1` through `rows * cols` in row-major order. A show plays one movie on one screen and occupies the half-open interval `[startTime, startTime + duration)`. Shows on the same screen must not overlap, though back-to-back shows are allowed. Seat availability belongs to a show, so two shows on the same screen have separate seat inventories. Booking is atomic: either every requested seat is claimed or nothing changes. A booking ID that has been used successfully stays used forever, even after the booking is canceled. A failed booking does not use up its ID. Canceled bookings remain in history with their original total price. Every failed mutation leaves state unchanged. Strings are case-sensitive and are only checked for being empty. The constructor `MovieBookingSystem()` starts with empty registries. Registration methods return `true` on success. Methods are called sequentially, and no concurrent threads, external payment service or wall-clock timing is required. Implement the following methods: - `addMovie(movieId: int, title: str, duration: int) -> bool` registers an immutable movie with a duration in minutes. Returns `false` for a duplicate or nonpositive `movieId`, an empty title, or a nonpositive duration. - `addTheatre(theatreId: int, city: str) -> bool` registers a theatre in a case-sensitive city. Returns `false` for a duplicate or nonpositive `theatreId` or an empty city. - `addScreen(screenId: int, theatreId: int, rows: int, cols: int) -> bool` registers a screen with a `rows x cols` layout (seats `1..rows*cols`, row-major) at an existing theatre. Returns `false` for a duplicate or nonpositive `screenId`, a missing theatre, or nonpositive `rows` or `cols`. - `addShow(showId: int, movieId: int, screenId: int, startTime: int, seatPrice: int) -> bool` schedules a show occupying `[startTime, startTime + duration)` on a screen. Returns `false` for a duplicate or nonpositive `showId`, a missing movie or screen, a negative `startTime`, a nonpositive `seatPrice`, or an overlap with another show on the same screen. - `findShows(movieId: int, city: str) -> List[int]` returns the ascending IDs of shows playing the movie in theatres located exactly in the given city, including sold-out shows. Returns `[]` if none match. - `getAvailableSeats(showId: int) -> List[int]` returns the ascending seat numbers not currently booked for the show. Returns `[]` for a missing or sold-out show. - `bookSeats(bookingId: int, showId: int, userId: int, seats: List[int]) -> int` atomically books all requested seats and returns `seatPrice * number of seats`. Returns `-1` for a nonpositive `bookingId` or `userId`, any `bookingId` previously used successfully (including canceled bookings), a missing show, an empty seat list, duplicate seat numbers, an out-of-range seat, or a seat already booked for this show. Failure changes no state and does not consume the booking ID. - `cancelBooking(bookingId: int, userId: int) -> bool` cancels a confirmed booking owned by `userId`, releasing its seats. Returns `false` for an unknown booking, the wrong user, or an already canceled booking. - `getBooking(bookingId: int) -> List[int]` returns `[showId, userId, totalPrice, status]`, where `status` is `1` for confirmed and `0` for canceled. Returns `[]` for an unknown booking. Example 1 (registration, scheduling and search): ```text MovieBookingSystem() addMovie(1, "Inception", 120) -> true addMovie(1, "Interstellar", 169) -> false (movie ID 1 is taken) addTheatre(10, "Pune") -> true addTheatre(11, "Mumbai") -> true addScreen(100, 10, 2, 3) -> true (seats 1..6) addScreen(101, 11, 1, 2) -> true (seats 1..2) addScreen(102, 12, 5, 5) -> false (theatre 12 does not exist) addShow(1000, 1, 100, 0, 200) -> true (occupies [0, 120)) addShow(1001, 1, 100, 60, 200) -> false ([60, 180) overlaps show 1000) addShow(1002, 1, 100, 120, 250) -> true ([120, 240) starts exactly when show 1000 ends) addShow(1003, 1, 101, 0, 150) -> true (different screen, no conflict) findShows(1, "Pune") -> [1000, 1002] findShows(1, "pune") -> [] (city match is case-sensitive) findShows(1, "Mumbai") -> [1003] ``` Example 2 (continues from the state at the end of Example 1): ```text bookSeats(1, 1000, 7, [2, 3]) -> 400 (2 seats at 200 each) bookSeats(2, 1000, 8, [3, 4]) -> -1 (seat 3 is taken, nothing changes) bookSeats(2, 1000, 8, [4, 5]) -> 400 (the failure did not use up ID 2) bookSeats(3, 1000, 8, [6, 6]) -> -1 (duplicate seat in the request) getAvailableSeats(1000) -> [1, 6] getAvailableSeats(1002) -> [1, 2, 3, 4, 5, 6] (same screen, separate inventory) cancelBooking(1, 8) -> false (booking 1 belongs to user 7) cancelBooking(1, 7) -> true (seats 2 and 3 are released) cancelBooking(1, 7) -> false (already canceled) getAvailableSeats(1000) -> [1, 2, 3, 6] bookSeats(1, 1000, 7, [2]) -> -1 (ID 1 was used successfully before) getBooking(1) -> [1000, 7, 400, 0] getBooking(2) -> [1000, 8, 400, 1] getBooking(3) -> [] (the failed request created nothing) ``` ```hint Validate, then write Picture a `bookSeats` request whose last seat is already taken. Your method must be able to return -1 at that point with nothing left behind, so think about when the first write happens. ``` ```hint Half-open intervals Write the overlap test for two shows and try it on a pair where one ends at exactly the minute the next one starts. ``` ```hint What a canceled booking keeps List what must still be stored after a cancellation so that the ID stays refused and `getBooking` still reports the original total. ``` ### Constraints & Assumptions - At most `10^4` total operations. - `-1 <= movieId, theatreId, screenId, showId, bookingId, userId <= 10^4` - `0 <= rows, cols <= 20` - `0 <= duration <= 300` - `-1 <= startTime <= 10^6` - `0 <= seatPrice <= 10^4` - `0 <= title.length, city.length <= 50` - `0 <= seats.length <= 400`, each entry in `[-1, 500]` - The values 0 and -1 are deliberately invalid inputs handled by the return rules. ### Clarifying Questions to Ask - Do users have to be registered before they book, or is any positive `userId` accepted? - Will movies, screens or shows ever be removed or rescheduled, so that the indexes would need delete paths? - With no clock in the system, should `findShows` return every scheduled show, including ones that would already have ended? ### What a Strong Answer Covers - A class model that puts seat inventory on the show and keeps the facade as the only public entry point. - Validation of every rule before any write, so failed calls leave state unchanged and do not consume a booking ID. - A half-open overlap check per screen that accepts back-to-back shows. - Cancellation that releases exactly the booking's seats, keeps the record, and blocks reuse of its ID. - Indexes that keep `findShows` and the overlap check cheap, with the time and space cost of each method. ### Follow-up Questions - How would you change `bookSeats` if many threads could book seats for the same show at once? - How would you add a temporary seat hold that expires if payment does not finish in time? - How would you speed up the overlap check for a screen with thousands of scheduled shows? - How would you support different prices for different rows or seat categories?

Overview: Design an in-memory movie ticket booking system that models movies, theatres, screens, shows and bookings behind one facade, with exact return rules for every method. It tests class modeling, non-overlapping show scheduling, all-or-nothing seat booking, and cancellation that keeps booking history intact.

Nov 24, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design an in-memory movie ticket booking system that models movies, theatres, screens, shows, seats and bookings behind a single public facade.

A movie can have many shows. A theatre belongs to one city and has many screens. Each screen has a fixed rows x cols layout whose seats are numbered 1 through rows * cols in row-major order. A show plays one movie on one screen and occupies the half-open interval [startTime, startTime + duration). Shows on the same screen must not overlap, though back-to-back shows are allowed. Seat availability belongs to a show, so two shows on the same screen have separate seat inventories.

Booking is atomic: either every requested seat is claimed or nothing changes. A booking ID that has been used successfully stays used forever, even after the booking is canceled. A failed booking does not use up its ID. Canceled bookings remain in history with their original total price. Every failed mutation leaves state unchanged. Strings are case-sensitive and are only checked for being empty.

The constructor MovieBookingSystem() starts with empty registries. Registration methods return true on success. Methods are called sequentially, and no concurrent threads, external payment service or wall-clock timing is required.

Implement the following methods:

  • addMovie(movieId: int, title: str, duration: int) -> bool registers an immutable movie with a duration in minutes. Returns false for a duplicate or nonpositive movieId , an empty title, or a nonpositive duration.
  • addTheatre(theatreId: int, city: str) -> bool registers a theatre in a case-sensitive city. Returns false for a duplicate or nonpositive theatreId or an empty city.
  • addScreen(screenId: int, theatreId: int, rows: int, cols: int) -> bool registers a screen with a rows x cols layout (seats 1..rows*cols , row-major) at an existing theatre. Returns false for a duplicate or nonpositive screenId , a missing theatre, or nonpositive rows or cols .
  • addShow(showId: int, movieId: int, screenId: int, startTime: int, seatPrice: int) -> bool schedules a show occupying [startTime, startTime + duration) on a screen. Returns false for a duplicate or nonpositive showId , a missing movie or screen, a negative startTime , a nonpositive seatPrice , or an overlap with another show on the same screen.
  • findShows(movieId: int, city: str) -> List[int] returns the ascending IDs of shows playing the movie in theatres located exactly in the given city, including sold-out shows. Returns [] if none match.
  • getAvailableSeats(showId: int) -> List[int] returns the ascending seat numbers not currently booked for the show. Returns [] for a missing or sold-out show.
  • bookSeats(bookingId: int, showId: int, userId: int, seats: List[int]) -> int atomically books all requested seats and returns seatPrice * number of seats . Returns -1 for a nonpositive bookingId or userId , any bookingId previously used successfully (including canceled bookings), a missing show, an empty seat list, duplicate seat numbers, an out-of-range seat, or a seat already booked for this show. Failure changes no state and does not consume the booking ID.
  • cancelBooking(bookingId: int, userId: int) -> bool cancels a confirmed booking owned by userId , releasing its seats. Returns false for an unknown booking, the wrong user, or an already canceled booking.
  • getBooking(bookingId: int) -> List[int] returns [showId, userId, totalPrice, status] , where status is 1 for confirmed and 0 for canceled. Returns [] for an unknown booking.

Example 1 (registration, scheduling and search):

MovieBookingSystem()
addMovie(1, "Inception", 120)        -> true
addMovie(1, "Interstellar", 169)     -> false   (movie ID 1 is taken)
addTheatre(10, "Pune")               -> true
addTheatre(11, "Mumbai")             -> true
addScreen(100, 10, 2, 3)             -> true    (seats 1..6)
addScreen(101, 11, 1, 2)             -> true    (seats 1..2)
addScreen(102, 12, 5, 5)             -> false   (theatre 12 does not exist)
addShow(1000, 1, 100, 0, 200)        -> true    (occupies [0, 120))
addShow(1001, 1, 100, 60, 200)       -> false   ([60, 180) overlaps show 1000)
addShow(1002, 1, 100, 120, 250)      -> true    ([120, 240) starts exactly when show 1000 ends)
addShow(1003, 1, 101, 0, 150)        -> true    (different screen, no conflict)
findShows(1, "Pune")                 -> [1000, 1002]
findShows(1, "pune")                 -> []      (city match is case-sensitive)
findShows(1, "Mumbai")               -> [1003]

Example 2 (continues from the state at the end of Example 1):

bookSeats(1, 1000, 7, [2, 3])        -> 400     (2 seats at 200 each)
bookSeats(2, 1000, 8, [3, 4])        -> -1      (seat 3 is taken, nothing changes)
bookSeats(2, 1000, 8, [4, 5])        -> 400     (the failure did not use up ID 2)
bookSeats(3, 1000, 8, [6, 6])        -> -1      (duplicate seat in the request)
getAvailableSeats(1000)              -> [1, 6]
getAvailableSeats(1002)              -> [1, 2, 3, 4, 5, 6]   (same screen, separate inventory)
cancelBooking(1, 8)                  -> false   (booking 1 belongs to user 7)
cancelBooking(1, 7)                  -> true    (seats 2 and 3 are released)
cancelBooking(1, 7)                  -> false   (already canceled)
getAvailableSeats(1000)              -> [1, 2, 3, 6]
bookSeats(1, 1000, 7, [2])           -> -1      (ID 1 was used successfully before)
getBooking(1)                        -> [1000, 7, 400, 0]
getBooking(2)                        -> [1000, 8, 400, 1]
getBooking(3)                        -> []      (the failed request created nothing)

Constraints & Assumptions

  • At most 10^4 total operations.
  • -1 <= movieId, theatreId, screenId, showId, bookingId, userId <= 10^4
  • 0 <= rows, cols <= 20
  • 0 <= duration <= 300
  • -1 <= startTime <= 10^6
  • 0 <= seatPrice <= 10^4
  • 0 <= title.length, city.length <= 50
  • 0 <= seats.length <= 400 , each entry in [-1, 500]
  • The values 0 and -1 are deliberately invalid inputs handled by the return rules.

Clarifying Questions to Ask Guidance

  • Do users have to be registered before they book, or is any positive userId accepted?
  • Will movies, screens or shows ever be removed or rescheduled, so that the indexes would need delete paths?
  • With no clock in the system, should findShows return every scheduled show, including ones that would already have ended?

What a Strong Answer Covers Guidance

  • A class model that puts seat inventory on the show and keeps the facade as the only public entry point.
  • Validation of every rule before any write, so failed calls leave state unchanged and do not consume a booking ID.
  • A half-open overlap check per screen that accepts back-to-back shows.
  • Cancellation that releases exactly the booking's seats, keeps the record, and blocks reuse of its ID.
  • Indexes that keep findShows and the overlap check cheap, with the time and space cost of each method.

Follow-up Questions Guidance

  • How would you change bookSeats if many threads could book seats for the same show at once?
  • How would you add a temporary seat hold that expires if payment does not finish in time?
  • How would you speed up the overlap check for a screen with thousands of scheduled shows?
  • How would you support different prices for different rows or seat categories?