Practice Design Underground System
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Problem Statement
UndergroundSystem records checkIn(id,stationName,t) and matching checkOut(id,stationName,t). getAverageTime(startStation,endStation) averages completed direct trips in that direction. Reverse trips are a separate route, and ongoing trips do not count. Events are consistent and chronological; queries have at least one completed matching trip. Practice approach: Map active IDs to starting station/time. On checkout, add the duration to a (total,count) pair keyed by the ordered route and remove the active entry. Each operation is expected O(1). Use a wide accumulator and divide at query time.
Source: Design Underground System on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
["UndergroundSystem","checkIn","checkOut","checkIn","getAverageTime","checkOut","getAverageTime"] [[],[1,"A",2],[1,"B",8],[2,"A",9],["A","B"],[2,"B",19],["A","B"]]
[null,null,null,null,6,null,8]
The ongoing second trip does not change the first average. Completed durations six and ten average eight.
Constraints
Related LeetCode Problems
Practice more problems similar to Design Underground System.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.