LLD Guide · Module 2: Problem prep · Lesson 02.08

Prep: Railway Reservation System

Confirmed, RAC and waiting list, berth preferences, and a promotion chain that must stay FIFO and atomic.

By Cruscible Engineering · Published

One cancellation can move three passengers. If those three moves aren't one step, someone ends up with two seats or none.

This lesson prepares you for Build a Railway Reservation System. It doesn't solve it.

1. What the railway reservation contract asks for

One train with three tiers of capacity: confirmed berths, RAC places, and a waiting list. Bookings fill them in that order, and a full train rejects. Cancelling a confirmed passenger frees a berth, and the queues move up behind them. All in memory.

MethodReturns / does
initialize(confirmed, rac, waiting)sets the capacities and clears everything
bookPassenger(name, age, gender, berthPreference)a unique passenger id, or null when all three tiers are full
cancelBooking(passengerId)true, then runs the promotion chain; false if unknown or already cancelled
getBookingDetails(passengerId)the passenger map (below), or null if unknown
getAvailableCounts(){confirmed, rac, waitingList} places left
getConfirmedPassengers()every CONFIRMED passenger
getRACQueue() / getWaitingList()those queues in FIFO order
getBookedCount(){CONFIRMED, RAC, WAITING} counts, excluding cancelled

A passenger map has passengerId, name, age, gender, berthPreference, allocatedBerth, seatNumber and status. RAC and waiting passengers have allocatedBerth = null and seatNumber = -1.

Figure: One confirmed cancellation: the first RAC passenger gets the berth, the first waiting passenger moves to RAC

2. Lessons to read before the railway reservation

3. Railway reservation design decisions to make first

  1. How do you assign berths? Five berth types must all exist. How do you lay out seat numbers and types for any confirmed count, and find a free berth of the preferred type quickly?
  2. What happens when the preferred type is gone? And with preference NONE, or a preference that isn't one of the six values?
  3. What exactly does each cancellation do? Write the chain for a confirmed, a RAC and a waiting passenger. Which berth does the promoted RAC passenger get?
  4. What makes the chain one step? Between freeing a berth and promoting, no other booking may grab it.
  5. What does a cancelled passenger look like afterwards? getBookingDetails still knows them; which status, and which counts and lists drop them?
  6. Which structures keep FIFO order for RAC and waiting, including removal from the middle when someone in the queue cancels?

4. Railway reservation pitfalls

⚠️ Trap: promotion must keep FIFO order: the first RAC passenger is confirmed, and the first waiting passenger moves to RAC.

⚠️ Trap: RAC and waiting passengers report allocatedBerth = null and seatNumber = -1, although the brief also mentions RAC passengers sharing side-lower berths. The returned map follows the constraint.

⚠️ Trap: an unknown berth preference isn't an error. It's treated as NONE.

⚠️ Trap: cancelling twice returns false the second time, and must not run the chain again.

💡 Tip: the injected KV store is optional; plain Java collections are allowed too.

Before you submit

Further reading

After you've tried it

Submit first. The full walkthrough of one solution: Building an Indian Railway Reservation System.

Open the problem