Design an Underground System
🚇 What is an underground system? It is the software behind the ticket gates of a metro. A rider taps a card when they enter a station and taps it again when they leave another one. From those two taps the system knows each trip: where it started, where it ended and how long it took. The operator wants one number out of all this: the average travel time between any two stations.
This is also a classic coding question (the LeetCode version is "Design Underground System"). The LeetCode statement promises that every call is valid. An LLD interview does not: a card can tap in twice, tap out without tapping in, or tap out twice, and you are expected to handle that. We cover the same core design and add those rules, then spend the rest of the time on the part that makes this a medium problem: choosing what to store so that the average is cheap, and keeping it correct when many gates tap at once.
Requirements
"Design the system for a metro's card gates. Riders check in at one station and check out at another. We want the average travel time between any two stations."
Clarifying Questions
You: Is the average for a pair of stations the same in both directions?
Interviewer: No. Trains on one line can be slower one way, so A to B and B to A are separate routes with separate averages.
You: What if a card taps in while it is already inside?
Interviewer: A card can only be in one place. Refuse the second tap and keep the first check-in.
You: And a card that taps out without having tapped in, or taps out twice?
Interviewer: Refuse it and don't record a trip. Tell the caller it failed.
You: What should the average be for a route nobody has ridden yet?
Interviewer: Return -1 so the dashboard can show "no data".
You: Do we need every single trip later, for example the median or the last hour?
Interviewer: Not for now. Only the average over all trips. Keep it fast: the dashboard asks for averages all the time.
You: How do we get the time?
Interviewer: Each tap comes with a timestamp. Times never go backwards.
You: Is there one gate, or many?
Interviewer: Many gates in many stations. Start with a single thread, and we will talk about concurrency after.
The answer about medians and the last hour decides the storage. "Only the average over all trips" means we do not have to keep every trip. That one sentence turns the problem from "store a growing list" into "keep two numbers per route".
💡 Interview tip: When the output is an aggregate (an average, a count, a total), ask which aggregates are needed and over what window. A running total answers "the average over all time" in constant space, but it cannot answer "the median" or "the last hour". Asking first saves you from building the wrong structure.
Final Requirements
checkIn(card, station, t)records that a card entered a station at timet. A card that is already checked in is refused, and its first check-in stays.checkOut(card, station, t)completes the card's trip and returns its duration. A card that is not checked in is refused with-1, and no trip is recorded.- After checking out, a card can check in again.
getAverageTime(start, end)returns the average duration of all completed trips fromstarttoend, or-1if there are none. Direction matters.- A trip may begin and end at the same station.
- The average is read far more often than trips are added, so it must be fast.
Out of scope
- Fares, balances and payment (we come back to fares in Extensibility).
- Lines, transfers and the train network: we only know where a trip started and ended.
- Cards that tap in and never tap out (also in Extensibility).