IRCTC Booking System

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2025-04-16

Problem Statement

Design the Low-Level Design (LLD) of a simplified IRCTC Train Booking System, which allows users to search trains, book tickets, cancel tickets, and check PNR status.


Your focus should be on designing classes, relationships, and interactions between entities like trains, passengers, bookings, seats, and payments — not the full backend architecture or database sharding (that would be part of HLD).


🎯 Goal


Design a system that can:


•Handle train and seat management.
•Manage ticket bookings and cancellations.
•Maintain real-time seat availability.
•Support multiple booking classes (e.g., Sleeper, AC1, AC2, General).
•Generate PNR (Passenger Name Record) for each confirmed booking.


✅ Functional Requirements


Train Management

•Each train has:
•Train number, name, source, destination, and route.
•Multiple coaches (Sleeper, AC1, AC2, etc.).
•Seat layout for each coach.
•Maintain train schedules (departure/arrival times).

Search Functionality

•Search available trains based on:
•Source and destination stations.
•Date of journey.
•Display seat availability and fare per class.

Booking Functionality

•A user can:
•Select train, class, and number of passengers.
•Reserve available seats.
•Make payment and confirm booking.
•Generate a unique PNR number for each booking.
•If seats are not available → Add passengers to waiting list.

Cancellation

•Allow cancellation of tickets.
•On cancellation:
•Update seat availability.
•Move next passenger from waiting list to confirmed list.

PNR Enquiry

•Fetch booking details (train info, seat, passenger list, status).
1.Payment Handling (Simplified)
•Mock payment service with success/failure response.


⚙️ Non-Functional Requirements


•Thread-safe booking (no two users can book the same seat).
•Consistent seat allocation during concurrent access.
•Scalable design to handle multiple trains and users.
•Extensible — future support for Tatkal, Dynamic pricing, etc.


🧱 Key Classes to Design


ClassResponsibility


Train


Represents a train with route, schedule, and list of coaches.


Coach


Represents a coach in a train (e.g., Sleeper, AC2) and manages its seats.


Seat


Represents individual seats with seat number, status (Available, Booked, WL).


Passenger


Represents user details (name, age, gender, ID).


Booking


Holds booking details like PNR, passengers, seat info, train info, fare, and status.


Payment


Handles payment processing and booking confirmation.


BookingManager / IRCTCSystem


Main controller that handles train search, booking, and cancellation logic.


WaitlistManager


Manages waiting list queues per train/class.


PNRGenerator


Utility class to generate unique PNR numbers.


🔄 Example Flow


Booking Flow:


1.User searches for trains from Delhi → Mumbai on 25 Oct 2025.
2.System lists available trains with available seats.
3.User selects a train and class (e.g., Sleeper).
4.System checks seat availability.
5.If seats available → confirm booking + generate PNR.
6.If not → add to waiting list.
7.Payment processed → booking confirmed.
8.Confirmation returned with PNR, coach, and seat details.


🧪 Example Input / Output


Input:

1 2BookTicket( 3 user = "Sahil", 4 trainNumber = 12951, 5 source = "Delhi", 6 destination = "Mumbai", 7 date = "25-10-2025", 8 class = "Sleeper", 9 passengers = 2 10)

Output:

1 2Booking Confirmed 3PNR: 8765432109 4Train: Rajdhani Express (12951) 5Seats: S1-23, S1-24 6Fare: ₹2400


If no seats:

1 2Booking Added to Waiting List 3WL Number: WL/03


🔐 Concurrency Example


If two users try to book the same seat:


•Use synchronized booking methods or locks to ensure atomic seat allocation.
•Once a seat is booked, it cannot be double-booked.


🧠 Possible Extensions


•Tatkal booking system.
•Integration with real payment gateway.
•Push notifications on seat confirmation.
•Dynamic fare pricing based on demand.
•Multi-leg journey (connecting trains).


✅ Summary:


Design the LLD for an IRCTC Train Booking System that supports:


•Searching trains
•Booking tickets with PNR generation
•Managing seat availability and waiting lists
•Cancellations and refunds


Focus on class relationships, seat allocation logic, and thread-safe booking design.

Solution

Comments