Design Underground System - Problem
Design an UndergroundSystem class that tracks customer travel times between different stations.
Implement these methods:
void checkIn(int id, string stationName, int t)- Customer with card ID checks in at station at time tvoid checkOut(int id, string stationName, int t)- Customer checks out from station at time tdouble getAverageTime(string startStation, string endStation)- Returns average travel time from start to end station
The system calculates averages from all previous trips between the same start and end stations. All check-ins will have corresponding check-outs, and times are consistent (check-out time > check-in time).
Input & Output
Example 1 — Basic Operations
$
Input:
operations = ["UndergroundSystem","checkIn","checkIn","checkIn","checkOut","checkOut","checkOut","getAverageTime","getAverageTime","checkIn","getAverageTime","checkOut","getAverageTime"], parameters = [[],[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]
💡 Note:
System tracks trips: Paradise→Cambridge (14 min), Leyton→Waterloo (15 min initially, then average with 14 min = 12.0 min)
Example 2 — Multiple Routes
$
Input:
operations = ["UndergroundSystem","checkIn","checkOut","getAverageTime","checkIn","checkOut","getAverageTime","checkIn","checkOut","getAverageTime"], parameters = [[],[10,"Leyton",3],[10,"Paradise",8],["Leyton","Paradise"],[11,"Paradise",10],[11,"Cambridge",20],["Paradise","Cambridge"],[12,"Cambridge",22],[12,"Leyton",30],["Cambridge","Leyton"]]
›
Output:
[null,null,null,5.00000,null,null,10.00000,null,null,8.00000]
💡 Note:
Three different routes: Leyton→Paradise (5 min), Paradise→Cambridge (10 min), Cambridge→Leyton (8 min)
Example 3 — Same Route Multiple Times
$
Input:
operations = ["UndergroundSystem","checkIn","checkOut","checkIn","checkOut","getAverageTime"], parameters = [[],[1,"A",0],[1,"B",10],[2,"A",5],[2,"B",13],["A","B"]]
›
Output:
[null,null,null,null,null,9.00000]
💡 Note:
Two trips on same route A→B: first takes 10 min, second takes 8 min, average = (10+8)/2 = 9.0
Constraints
- 1 ≤ id, t ≤ 106
- 1 ≤ stationName.length, startStation.length, endStation.length ≤ 10
- All strings contain only lowercase English letters and digits
- There will be at most 2 × 104 calls in total
- All calls to checkIn and checkOut are consistent
- getAverageTime is called with stations that have at least one trip
Visualization
Tap to expand
Understanding the Visualization
1
Check In
Passengers tap in with ID at start station
2
Check Out
Passengers tap out at destination, system records trip
3
Query Average
System returns average time for any route instantly
Key Takeaway
🎯 Key Insight: Use hash maps for O(1) operations - track active trips separately from route statistics
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code