Design a Flight or Train Booking System
✈️ What is a flight booking system? It is the software behind the "Book a flight" page. A traveller searches for flights between two cities, picks a seat class, books seats for their group and gets seat numbers. Later they may cancel and get some money back. On the other side, the airline adds flights to the schedule and sometimes has to cancel one.
Interviewers ask this problem under several names: an airline reservation system, an airline management system, or a train ticket booking system. They are the same design. A flight and a train both have a route, a departure time and a fixed number of seats split into classes (economy and business on a plane, sleeper and first class on a train). We design for flights, cover the airline's side (adding and cancelling flights) in the requirements, and show what changes for trains with stops along the way in Extensibility.
Requirements
"Design a flight booking system. Users search for flights, book seats in a class like economy or business, and can cancel."
The prompt leaves out almost everything that decides the design. Here are the questions worth asking, and the answers we will code against.
Clarifying Questions
You: What does a search take as input, and what comes back?
Interviewer: An origin, a destination, a seat class and the number of passengers. Return the flights on that route that have not left yet and still have enough free seats in that class, earliest departure first.
You: What seat classes are there? Does each one have its own price?
Interviewer: Economy, business and first. Each flight sets how many seats it has in each class and the fare for one seat in that class. Some flights have no first class at all.
You: Do travellers pick their own seats?
Interviewer: Not in this version. The system gives a booking the lowest-numbered free seats in its class. A group books together: either everyone gets a seat in the same class, or the booking fails.
You: What happens when someone cancels?
Interviewer: They get a full refund if the flight leaves at least 24 hours later, and half the price otherwise. After departure you cannot cancel. Finance changes these rules every few months, by the way.
You: What can the airline do?
Interviewer: Add flights, and cancel a whole flight, for example when an aircraft is grounded. Then every passenger on it gets a full refund, whatever the normal rules say.
You: Do we take payments, or store passenger names and passports?
Interviewer: No. A booking counts as paid when it is made, and passenger details are another team's problem.
You: Do we need connecting flights?
Interviewer: Direct flights only. And time is passed in with each call, in minutes.
Two answers shape most of the code. "Lowest-numbered free seats" means seat assignment has a precise rule we can test, and freed seats must be handed out again in the right order. "Finance changes these rules" tells us exactly where the design should bend: the refund rule.
💡 Interview tip: Ask who the users are. Here there are two: travellers (search, book, cancel) and the airline (add and cancel flights). Naming both early stops you from forgetting the airline's side, which is where the interesting "cancel everyone" logic lives.
Final Requirements
- The airline can add a flight with an id, an origin, a destination, a departure time, and for each seat class (economy, business, first) a number of seats and a fare per seat. Flight ids are unique.
- A traveller can search by origin, destination, seat class and number of passengers. The result lists flights on that route that are not cancelled, have not departed and have enough free seats in that class, sorted by departure time, then flight id.
- A traveller can book seats for a group in one class of one flight. The booking gets the lowest-numbered free seats of that class and costs the fare times the number of passengers. If there are not enough free seats, or the flight is cancelled or has departed, nothing is booked.
- A traveller can see the seats of their booking (
E1,B2, ...). - A traveller can cancel their own booking before departure. The refund is the full price at least 24 hours before departure, and half (rounded down) after that. Freed seats can be booked again.
- The airline can cancel a flight. Every live booking on it is cancelled with a full refund, and the flight disappears from search.
- Refund rules change often, so swapping the refund rule must not touch the rest of the system.
Out of scope
- Payments, passenger details, tickets and boarding passes.
- Connecting flights and multi-city trips.
- Choosing a specific seat (we come back to this in Extensibility).
- Several users booking at the same time (also in Extensibility).