Design an Airport Runway Controller
🛫 What is a runway controller? It is the part of air traffic control that decides who uses a runway next. Planes in the air ask to land, planes at the gate ask to take off, and a runway can only hold one of them at a time. The controller keeps a waiting line in priority order and clears the next flight the moment a runway is free.
You may hear this as "design an air traffic controller", "design a runway scheduler" or "manage a takeoff and landing queue with priorities". They are the same design: a few shared resources (runways), a stream of requests, and a priority rule for who goes next. The full air traffic control job (radar, routes, holding patterns) is far bigger than an interview; we model the runway decision, and come back to telling pilots about clearances in Extensibility.
Requirements
"Design the system that schedules takeoffs and landings at an airport. There are a few runways, and some flights have priority."
The interesting words are "a few runways" and "priority". Everything else comes from questions.
Clarifying Questions
You: Can any runway handle any flight, and does a runway do one thing at a time?
Interviewer: Assume the runways are identical, and each one handles one landing or one takeoff at a time.
You: How long does a flight hold a runway?
Interviewer: A fixed time per operation: a landing takes longer than a takeoff. Both are set when the system starts.
You: Who goes first when several flights are waiting?
Interviewer: Emergencies first. Then landings, because a plane in the air is burning fuel and cannot just wait at a gate. Then takeoffs. Within the same kind, first come first served.
You: Who can declare an emergency, and does the order among emergencies matter?
Interviewer: A flight waiting to land. A plane on the ground with a problem goes back to the gate instead. If two planes declare, the first one to declare goes first.
You: Can a flight leave the queue?
Interviewer: Yes. A landing might divert to another airport, a takeoff might go back to the gate. Either way it cancels.
You: How do we know the time?
Interviewer: Every call comes with a timestamp. Timestamps never go backwards.
You: If a runway frees up while no call is happening, is the next flight cleared right then, or at the next call?
Interviewer: Right then. Runways should never sit idle while someone waits.
The last answer is the one that shapes the code. It means the controller has to know about moments when nothing calls it. We will see how to handle that without a background thread.
💡 Interview tip: When a prompt says "priorities", ask for the exact order and how ties break inside one priority level. "Landings first" leaves open whether two landings go by request time or by fuel left, and those are different data structures.
Final Requirements
- The airport has
Nidentical runways, numbered from 0. Each runway handles one operation at a time: a landing holds it for a fixed landing time, a takeoff for a fixed takeoff time. - A flight requests a runway for a landing or a takeoff. A flight that is already waiting or still on a runway cannot request again.
- Waiting flights are served in this order: emergencies, then landings, then takeoffs. Inside landings and inside takeoffs, the earlier request goes first. Emergencies go in the order they were declared.
- Only a waiting landing can declare an emergency, and only once.
- A waiting flight can cancel. A cleared flight cannot.
- Whenever a runway is free and someone is waiting, the best waiting flight is cleared at that moment. It gets the runway that frees up soonest; among free runways, the lowest number.
- Anyone can ask which runway a flight was cleared to and who is next in line.
Out of scope
- Taxiways, gates, airspace and radar.
- Weather, runway direction and closures.
- Different runway lengths and aircraft sizes (we come back to this in Extensibility).
- Talking to pilots; we only decide and record clearances.