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 t
  • void checkOut(int id, string stationName, int t) - Customer checks out from station at time t
  • double 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
Design Underground System INPUT Check-In Records: ID:45 | Leyton | t=3 ID:32 | Paradise | t=8 ID:27 | Leyton | t=10 Check-Out Records: ID:45 | Waterloo | t=15 ID:27 | Waterloo | t=20 ID:32 | Cambridge | t=22 Station Network: Leyton Waterloo Paradise Cambridge ALGORITHM STEPS 1 checkIn() Store {id: (station, time)} travelers[45] = ("Leyton", 3) travelers[32] = ("Paradise", 8) 2 checkOut() Calc trip time, update stats trip = 15 - 3 = 12 (Leyton-Waterloo) routes[key].add(time) 3 Route Statistics Track total_time + count Leyton-Waterloo: sum=22, n=2 Paradise-Cambridge: sum=14, n=1 (Updated after each checkout) 4 getAverageTime() Return total / count avg = sum / count Leyton-Waterloo: 22/2 = 11.0 FINAL RESULT Query Results: getAverage("Paradise","Cambridge") 14.00 getAverage("Leyton","Waterloo") 11.00 After ID:10 trip (14 mins) 12.00 Output Array: [null, null, null, null, null, null, null, 14.0, 11.0, null, 11.0, null, 12.0] OK - All queries matched! O(1) average time per query Key Insight: Route Statistics Optimization Use two hash maps: (1) travelers{id} --> (station, time) for active check-ins, (2) routes{start-end} --> (total_time, count) for running statistics. This avoids storing individual trips and enables O(1) average calculation. TutorialsPoint - Design Underground System | Route Statistics Approach Time: O(1) per operation | Space: O(P + S^2) where P=passengers, S=stations
Asked in
Amazon 28 Facebook 15 Google 12 Apple 8
98.5K Views
Medium Frequency
~25 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen