Algo-Sim › Low-Level Design

Movie Ticket Booking (BookMyShow)

Medium · Low-Level Design · step through it in Java, Python and C++ on your own input

Seats are held for a few seconds while a user pays, then confirmed. The hold is a lock with an expiry, and check-then-hold runs atomically so no seat is sold twice.

Problem

Design seat booking for one show with 15 seats (0–14). hold(user, seat, t) reserves a seat for 5 seconds and returns true, or false if it is booked or another hold on it has not expired. confirm(user, t) books every seat the user still holds at time t and returns how many. Holds that expire free the seat for others. Calls arrive in time order and may come from many threads.

Constraints

Edge cases to test

Follow-up questions

How do you make holds safe across servers?
An atomic conditional update in the database (UPDATE … WHERE status = free) or a distributed lock with an expiry.
Optimistic or pessimistic locking?
Optimistic (a version check) suits low contention; popular shows may need pessimistic locks or a queue per show.

Solution

Java

class BookingService {
    static final int HOLD = 5;                  // seconds a held seat stays reserved
    private final int[] holder, heldUntil;      // holder 0: nobody
    private final boolean[] booked;

    public BookingService(int seats) {
        holder = new int[seats];
        heldUntil = new int[seats];
        booked = new boolean[seats];
    }

    /** synchronized: two users must never both get the same seat. */
    public synchronized boolean hold(int user, int seat, int t) {
        boolean free = !booked[seat] && (holder[seat] == 0 || heldUntil[seat] <= t);
        if (!free) return false;
        holder[seat] = user;
        heldUntil[seat] = t + HOLD;
        return true;
    }

    /** Books every seat the user still holds at time t (after payment). */
    public synchronized int confirm(int user, int t) {
        int count = 0;
        for (int s = 0; s < booked.length; s++) {
            if (holder[s] == user && !booked[s] && heldUntil[s] > t) {
                booked[s] = true;
                count++;
            }
        }
        return count;
    }
}

Python

import threading


class BookingService:
    HOLD = 5                                # seconds a held seat stays reserved

    def __init__(self, seats):
        self.holder = [0] * seats           # 0: nobody
        self.held_until = [0] * seats
        self.booked = [False] * seats
        self.lock = threading.Lock()

    # The lock makes check-then-hold atomic: two users never both get a seat.
    def hold(self, user, seat, t):
        with self.lock:
            free = not self.booked[seat] and (self.holder[seat] == 0 or self.held_until[seat] <= t)
            if not free:
                return False
            self.holder[seat] = user
            self.held_until[seat] = t + self.HOLD
            return True

    # Books every seat the user still holds at time t (after payment).
    def confirm(self, user, t):
        with self.lock:
            count = 0
            for s in range(len(self.booked)):
                if self.holder[s] == user and not self.booked[s] and self.held_until[s] > t:
                    self.booked[s] = True
                    count += 1
            return count

C++

#include <bits/stdc++.h>
using namespace std;

class BookingService {
  public:
    static constexpr int HOLD = 5; // seconds a held seat stays reserved
    vector<int> holder, heldUntil; // holder 0: nobody
    vector<bool> booked;

    BookingService(int seats) {
        holder = vector<int>(seats);
        heldUntil = vector<int>(seats);
        booked = vector<bool>(seats);
    }

    /** synchronized: two users must never both get the same seat. */
    mutex seatsMutex;
    bool hold(int user, int seat, int t) {
        lock_guard<mutex> lock(seatsMutex);
        bool free = !booked[seat] && (holder[seat] == 0 || heldUntil[seat] <= t);
        if (!free)
            return false;
        holder[seat] = user;
        heldUntil[seat] = t + HOLD;
        return true;
    }

    /** Books every seat the user still holds at time t (after payment). */
    int confirm(int user, int t) {
        lock_guard<mutex> lock(seatsMutex);
        int count = 0;
        for (int s = 0; s < int(booked.size()); s++) {
            if (holder[s] == user && !booked[s] && heldUntil[s] > t) {
                booked[s] = true;
                count++;
            }
        }
        return count;
    }
};

More Low-Level Design problems