Imagine you're tasked with building the tracking system for a massive underground railway network like the London Underground or NYC Subway. Your system needs to track millions of passenger journeys and provide real-time analytics about travel times between stations.
You'll implement an UndergroundSystem class that handles three core operations:
- Check-In: When a passenger taps their card at a station entrance
- Check-Out: When a passenger taps their card at their destination
- Analytics: Calculate average travel times between any two stations
The system must efficiently track passenger movements and compute travel statistics from historical data. Think of it as building the brain behind those "Average journey time: 23 minutes" displays you see in subway stations!
Key Requirements:
- Passengers can only be checked into one location at a time
- Travel times are directional (A→B may differ from B→A)
- Average calculations use all historical journeys between two stations
- The system must handle real-time queries efficiently
Input & Output
Visualization
Time & Space Complexity
All operations (checkIn, checkOut, getAverageTime) run in constant time using hash table lookups and updates
P is number of active passengers, R is number of unique routes. Much more efficient than storing all individual trips
Constraints
- 1 ≤ id, t ≤ 106
- 1 ≤ stationName.length, startStation.length, endStation.length ≤ 10
- All strings consist of uppercase and lowercase English letters and digits
- At most 2 × 104 calls in total will be made to checkIn, checkOut, and getAverageTime
- Answers within 10-5 of the actual answer will be accepted