Design Underground System

An underground railway tracks how long riders take to travel between stations, and reports the average travel time from one station to another. - `void checkIn(int id, string stationName, int t)`: the rider with card `id` enters at `stationName` at time `t`. A rider can be checked in to only one place at a time. - `void checkOut(int id, string stationName, int t)`: the rider with card `id` leaves at `stationName` at time `t`. - `double getAverageTime(string startStation, string endStation)`: returns the average time of all completed trips that started at `startStation` and ended at `endStation`. Trips from `endStation` to `startStation` are a different route. All calls are consistent: check-outs follow a matching check-in, times increase, and an average is only requested for a route with at least one completed trip. Answers within $10^{-5}$ of the true value are accepted.

30:00

Design Underground System
medium
Free written solution
Topics
Companies

An underground railway tracks how long riders take to travel between stations, and reports the average travel time from one station to another.

  • void checkIn(int id, string stationName, int t): the rider with card id enters at stationName at time t. A rider can be checked in to only one place at a time.
  • void checkOut(int id, string stationName, int t): the rider with card id leaves at stationName at time t.
  • double getAverageTime(string startStation, string endStation): returns the average time of all completed trips that started at startStation and ended at endStation. Trips from endStation to startStation are a different route.

All calls are consistent: check-outs follow a matching check-in, times increase, and an average is only requested for a route with at least one completed trip. Answers within 10−510^{-5} of the true value are accepted.

Example 1:
Input: {"commands":["UndergroundSystem","checkIn","checkIn","checkIn","checkOut","checkOut","checkOut","getAverageTime","getAverageTime","checkIn","getAverageTime","checkOut","getAverageTime"],"values":[[],[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.0,11.0,null,11.0,null,12.0]
Constraints:
  • 1≤id,t≤1061 \leq \text{id}, t \leq 10^6

  • 1≤stationName.length,startStation.length,endStation.length≤101 \leq \text{stationName.length}, \text{startStation.length}, \text{endStation.length} \leq 10

  • Station names consist of uppercase and lowercase English letters and digits.

  • At most 2×1042 \times 10^4 calls in total are made to checkIn, checkOut and getAverageTime.

  • Answers within 10−510^{-5} of the actual value are accepted.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"commands":["UndergroundSystem","checkIn","checkIn","checkIn","checkOut","checkOut","checkOut","getAverageTime","getAverageTime","checkIn","getAverageTime","checkOut","getAverageTime"],"values":[[],[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"]]}
new()
in(45,Leyton,3)
in(32,Paradise,8)
in(27,Leyton,10)
out(45,Waterloo,15)
out(27,Waterloo,20)
out(32,Cambridge,22)
avg(Paradise,Cambridge)
avg(Leyton,Waterloo)

…

Two hash maps: one for riders who are still inside (id → entry station and time), one for route totals ((start, end) → total time, trip count).

riders inside (check_ins: id → entry station, time)

nobody is riding

route totals ((start, end) → total, count)

route

total

count

avg

no completed trips yet

Returned values

[null]
Variables
No variables to display
DepthFunction Call
Stack empty
0/17