Skip to content

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.

Medium#1396
LeetCode Problem

Design Underground System

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

Input
["UndergroundSystem","checkIn","checkOut","checkIn","getAverageTime","checkOut","getAverageTime"]
[[],[1,"A",2],[1,"B",8],[2,"A",9],["A","B"],[2,"B",19],["A","B"]]
Output
[null,null,null,null,6,null,8]
Explanation

The ongoing second trip does not change the first average. Completed durations six and ten average eight.

Constraints

1 <= id, t <= 10^6
Station names have 1 to 10 alphanumeric English characters.
At most 2 * 10^4 total calls.
Checkout occurs strictly after the matching check-in.
Accepted error is at most 10^-5.

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.