Design Underground System - Video Solutions
Design Underground System - Leetcode 1396 - Python
Design Underground System - Video Solution
Watch 10 video solutions for Design Underground System, a medium level problem involving Hash Table, String, Design. This walkthrough by NeetCodeIO has 11,893 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Problem Statement
An underground railway system is keeping track of customer travel times between different stations. They are using this data to calculate the average time it takes to travel from one station to another.
Implement the UndergroundSystem class:
void checkIn(int id, string stationName, int t)- A customer with a card ID equal to
id, checks in at the stationstationNameat timet. - A customer can only be checked into one place at a time.
- A customer with a card ID equal to
void checkOut(int id, string stationName, int t)- A customer with a card ID equal to
id, checks out from the stationstationNameat timet.
- A customer with a card ID equal to
double getAverageTime(string startStation, string endStation)- Returns the average time it takes to travel from
startStationtoendStation. - The average time is computed from all the previous traveling times from
startStationtoendStationthat happened directly, meaning a check in atstartStationfollowed by a check out fromendStation. - The time it takes to travel from
startStationtoendStationmay be different from the time it takes to travel fromendStationtostartStation. - There will be at least one customer that has traveled from
startStationtoendStationbeforegetAverageTimeis called.
- Returns the average time it takes to travel from
You may assume all calls to the checkIn and checkOut methods are consistent. If a customer checks in at time t1 then checks out at time t2, then t1 < t2. All events happen in chronological order.
Example 1:
Input
["UndergroundSystem","checkIn","checkIn","checkIn","checkOut","checkOut","checkOut","getAverageTime","getAverageTime","checkIn","getAverageTime","checkOut","getAverageTime"]
[[],[45,"Leyton",3],[32,"Paradise",8],[27,"Leyton",10],[45,"Waterloo",15],[27,"Waterloo",20],[32,"Cambridge",22],["Paradise","Cambridge"],["Leyton","Waterloo"],[10,"Leyton",24],["Leyton","Waterloo"],[10,"Waterloo",38],["Leyton","Waterloo"]]
Output
[null,null,null,null,null,null,null,14.00000,11.00000,null,11.00000,null,12.00000]
Explanation
UndergroundSystem undergroundSystem = new UndergroundSystem();
undergroundSystem.checkIn(45, "Leyton", 3);
undergroundSystem.checkIn(32, "Paradise", 8);
undergroundSystem.checkIn(27, "Leyton", 10);
undergroundSystem.checkOut(45, "Waterloo", 15); // Customer 45 "Leyton" -> "Waterloo" in 15-3 = 12
undergroundSystem.checkOut(27, "Waterloo", 20); // Customer 27 "Leyton" -> "Waterloo" in 20-10 = 10
undergroundSystem.checkOut(32, "Cambridge", 22); // Customer 32 "Paradise" -> "Cambridge" in 22-8 = 14
undergroundSystem.getAverageTime("Paradise", "Cambridge"); // return 14.00000. One trip "Paradise" -> "Cambridge", (14) / 1 = 14
undergroundSystem.getAverageTime("Leyton", "Waterloo"); // return 11.00000. Two trips "Leyton" -> "Waterloo", (10 + 12) / 2 = 11
undergroundSystem.checkIn(10, "Leyton", 24);
undergroundSystem.getAverageTime("Leyton", "Waterloo"); // return 11.00000
undergroundSystem.checkOut(10, "Waterloo", 38); // Customer 10 "Leyton" -> "Waterloo" in 38-24 = 14
undergroundSystem.getAverageTime("Leyton", "Waterloo"); // return 12.00000. Three trips "Leyton" -> "Waterloo", (10 + 12 + 14) / 3 = 12
Example 2:
Input
["UndergroundSystem","checkIn","checkOut","getAverageTime","checkIn","checkOut","getAverageTime","checkIn","checkOut","getAverageTime"]
[[],[10,"Leyton",3],[10,"Paradise",8],["Leyton","Paradise"],[5,"Leyton",10],[5,"Paradise",16],["Leyton","Paradise"],[2,"Leyton",21],[2,"Paradise",30],["Leyton","Paradise"]]
Output
[null,null,null,5.00000,null,null,5.50000,null,null,6.66667]
Explanation
UndergroundSystem undergroundSystem = new UndergroundSystem();
undergroundSystem.checkIn(10, "Leyton", 3);
undergroundSystem.checkOut(10, "Paradise", 8); // Customer 10 "Leyton" -> "Paradise" in 8-3 = 5
undergroundSystem.getAverageTime("Leyton", "Paradise"); // return 5.00000, (5) / 1 = 5
undergroundSystem.checkIn(5, "Leyton", 10);
undergroundSystem.checkOut(5, "Paradise", 16); // Customer 5 "Leyton" -> "Paradise" in 16-10 = 6
undergroundSystem.getAverageTime("Leyton", "Paradise"); // return 5.50000, (5 + 6) / 2 = 5.5
undergroundSystem.checkIn(2, "Leyton", 21);
undergroundSystem.checkOut(2, "Paradise", 30); // Customer 2 "Leyton" -> "Paradise" in 30-21 = 9
undergroundSystem.getAverageTime("Leyton", "Paradise"); // return 6.66667, (5 + 6 + 9) / 3 = 6.66667
Constraints:
1 <= id, t <= 1061 <= stationName.length, startStation.length, endStation.length <= 10- All strings consist of uppercase and lowercase English letters and digits.
- There will be at most
2 * 104calls in total tocheckIn,checkOut, andgetAverageTime. - Answers within
10-5of the actual value will be accepted.
Approach Overview
Problem Overview: You need to design a transit tracking system that records when passengers check in and check out of stations. The system must return the average travel time between two stations using historical trip data.
Approach 1: Two Dictionary Method (O(1) per operation, O(n) space)
This approach keeps two hash maps. The first map tracks active trips using id → (stationName, time). When a passenger checks in, store their starting station and timestamp. The second map aggregates completed trips using (startStation, endStation) → (totalTime, tripCount). During checkout, look up the passenger’s start data, compute the trip duration, and update the aggregated totals. The average travel time is simply totalTime / tripCount, retrieved in constant time using a hash lookup.
The key insight is separating active journeys from historical statistics. Active trips require quick lookups by passenger ID, while averages require aggregation by station pair. Using hash tables for both tasks keeps every operation—check-in, check-out, and average query—at constant time. This method is clean, scalable, and mirrors how production analytics pipelines maintain running aggregates.
Approach 2: List Method with String Keys (O(1) per operation, O(n) space)
This variation stores aggregated trip statistics using a single key built from the station names, typically something like "start#end". The active trips are still tracked with an id → (station, time) map, but the historical data uses a string key instead of a tuple or structured pair.
When a passenger checks out, compute the trip duration and update the aggregated record stored under the combined key. A small list or array holds the cumulative travel time and trip count. Queries for the average travel time simply retrieve the aggregated values and compute the ratio.
This design works well in languages where composite keys are inconvenient or where string concatenation is simpler than creating pair objects. The tradeoff is slightly less structured data management, but the algorithmic behavior remains identical: constant-time updates and lookups using hash maps and simple string keys.
Recommended for interviews: The Two Dictionary Method is the expected solution. It demonstrates strong understanding of system design patterns, incremental aggregation, and hash-based lookups. Mentioning the separation between active sessions and aggregated metrics signals clear reasoning. The string-key variant is still valid but mainly reflects language-specific implementation convenience rather than a conceptual improvement.
Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Two Dictionary Method | O(1) per operation | O(n) | Standard solution for interviews and scalable production-style design |
| List Method with String Keys | O(1) per operation | O(n) | Useful when composite map keys are inconvenient and string keys are simpler |