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.
| Method | Returns / 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
- The train is full: P1 to P3 confirmed, P4 and P5 in RAC, P6 and P7 waiting.
- P2 cancels: P4, the first RAC passenger, gets the berth, and P6, the first waiting passenger, moves to RAC.
- P5 then cancels from RAC: P7 moves to RAC.
2. Lessons to read before the railway reservation
- 01.05 State machines: each passenger is WAITING, RAC, CONFIRMED or CANCELLED, with fixed legal moves.
- 01.02 Thread-safety: capacity is an invariant that concurrent bookings must not break.
3. Railway reservation design decisions to make first
- How do you assign berths? Five berth types must all exist. How do you lay out seat numbers and types for any
confirmedcount, and find a free berth of the preferred type quickly? - What happens when the preferred type is gone? And with preference
NONE, or a preference that isn't one of the six values? - 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?
- What makes the chain one step? Between freeing a berth and promoting, no other booking may grab it.
- What does a cancelled passenger look like afterwards?
getBookingDetailsstill knows them; which status, and which counts and lists drop them? - 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 = nullandseatNumber = -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
falsethe 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
- Bookings fill confirmed, then RAC, then waiting, and return
nullwhen all are full - The preferred berth is used when one is free; otherwise any free berth
- All five berth types exist for any confirmed capacity
- RAC and waiting passengers have
allocatedBerth = nullandseatNumber = -1 - Cancelling a confirmed passenger confirms the first RAC passenger and moves the first waiting passenger to RAC
- Cancelling a RAC passenger moves the first waiting passenger to RAC; cancelling a waiting passenger just removes them
- A cancelled passenger stays visible with status
CANCELLED, and cancelling again returnsfalse getAvailableCountsandgetBookedCountalways add up to the capacities- RAC and waiting lists come back in FIFO order
- Concurrent bookings and cancellations never exceed capacity or skip the queue
Further reading
After you've tried it
Submit first. The full walkthrough of one solution: Building an Indian Railway Reservation System.