Practice/Ramp/Leetcode 1396. Design Underground System
Leetcode 1396. Design Underground System
CodingMust
Problem
Design and implement a transit tracking system that monitors passenger movements through a transportation network. Your system needs to track when passengers enter and exit stations, then calculate the average travel time between any two stations.
The system should support three operations:
- checkIn(passenger_id, station_name, timestamp): Record that a passenger with the given ID checked in at the specified station at the given time.
- checkOut(passenger_id, station_name, timestamp): Record that a passenger with the given ID checked out at the specified station at the given time.
- getAverageTime(start_station, end_station): Return the average travel time for all trips that went directly from start_station to end_station.
You can assume:
- A passenger can only be checked in at one station at a time
- Each passenger checks out before checking in again
- All queries to getAverageTime will have at least one completed trip for that route
Requirements
- Implement the TransitTracker class with the three methods described above
- Track active check-ins for passengers currently in transit
- Maintain cumulative statistics (total time and trip count) for each station pair
- Calculate averages efficiently without iterating through all past trips
- Return the average as a floating-point number
Constraints
- 1 ≤ passenger_id, timestamp ≤ 10^6
- 1 ≤ station_name.length ≤ 10
- Station names consist of uppercase and lowercase English letters
- At most 2 × 10^4 total operations will be performed
- getAverageTime is only called when at least one trip exists for the route
- All checkIn/checkOut operations are valid (no invalid sequences)
Examples
Example 1:
`
operations: ["checkIn", "checkOut", "getAverageTime"]
params: [[45, "CentralStation", 3], [45, "WestTerminal", 21], ["CentralStation", "WestTerminal"]]
Output: 18.0
Explanation: Passenger 45 checks in at CentralStation at time 3 and checks out at WestTerminal at time 21. The trip duration is 21 - 3 = 18 minutes. Since this is the only trip on this route, the average is 18.0.
`
Example 2:
`
operations: ["checkIn", "checkIn", "checkOut", "checkOut", "getAverageTime"]
params: [[10, "StationA", 5], [20, "StationA", 10], [10, "StationB", 15], [20, "StationB", 25], ["StationA", "StationB"]]
Output: 12.5
Explanation:
- Passenger 10: checks in at StationA (time 5), checks out at StationB (time 15). Duration = 10 minutes.
- Passenger 20: checks in at StationA (time 10), checks out at StationB (time 25). Duration = 15 minutes.
- Average time for StationA → StationB = (10 + 15) / 2 = 12.5 minutes.
`
Example 3:
`
operations: ["checkIn", "checkOut", "checkIn", "checkOut", "getAverageTime", "getAverageTime"]
params: [[1, "X", 0], [1, "Y", 10], [1, "Y", 20], [1, "Z", 35], ["X", "Y"], ["Y", "Z"]]
Output: 10.0, 15.0
Explanation:
- Passenger 1 travels X → Y taking 10 minutes
- Passenger 1 later travels Y → Z taking 15 minutes
- These are tracked as separate routes with their respective averages
`