The Design Underground System coding problem involves tracking the travel times of customers in a subway system. You need to implement checkIn(id, stationName, t), checkOut(id, stationName, t), and getAverageTime(startStation, endStation). The average time is calculated for all completed journeys between two specific stations.
Companies like Goldman Sachs and Bloomberg use this to test your ability to maintain multiple types of state. It evaluations your skill in using Hash Tables to store transient data (customers currently checked in) versus aggregated data (total times and counts between station pairs). It’s a practical test of data modeling and clean class design.
This problem relies on two Hash Tables.
customerID to their (startStation, checkInTime). This tracks active journeys.(startStation, endStation) pair to a (totalTime, count). This stores the aggregated data needed for averages.
When a user checks out, you retrieve their check-in info, calculate the travel duration, and update the statistics map.getAverageTime is called (). A better way is to maintain a running sum and count ().When a problem asks for an "average," always maintain two variables: sum and count. This allows you to return the result instantly without re-traversing the history.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Encode and Decode TinyURL | Medium | Solve | |
| Design Log Storage System | Medium | Solve | |
| Design SQL | Medium | Solve | |
| Time Based Key-Value Store | Medium | Solve | |
| Unique Word Abbreviation | Medium | Solve |