Underground System Design

An underground railway system is keeping track of customer travel times between different stations. They are using this data to calculate the average time it takes to travel from one station to another.

Implement the UndergroundSystem class:

  • UndergroundSystem(): Initializes the UndergroundSystem object.
  • void checkIn(int id, String stationName, int t): A customer with a card ID equal to id, checks in at the station stationName at time t. A customer can only be checked into one place at a time.
  • void checkOut(int id, String stationName, int t): A customer with a card ID equal to id, checks out from the station stationName at time t.
  • double getAverageTime(String startStation, String endStation): Returns the average time it takes to travel from startStation to endStation.

The average time is computed from all the previous traveling times from startStation to endStation that happened directly, meaning a check in at startStation followed by a check out from endStation. The time it takes to travel from startStation to endStation may be different from the time it takes to travel from endStation to startStation.

Example 1:

Input: ["UndergroundSystem", "checkIn", "checkOut", "getAverageTime", "checkIn", "checkOut", "getAverageTime"]

[[], [10, "Uptown", 3], [10, "Midtown", 8], ["Uptown", "Midtown"], [20, "Uptown", 10], [20, "Midtown", 22], ["Uptown", "Midtown"]]

Output: [null, null, null, 5.00000, null, null, 8.50000]

Explanation: Customer 10 checks in at "Uptown" at time 3 and checks out at "Midtown" at time 8. This trip takes 5 units of time, making the average for this route 5.0. Later, customer 20 checks in at "Uptown" at time 10 and checks out at "Midtown" at time 22, taking 12 units of time. The system calculates the new average time for the "Uptown" to "Midtown" route by adding the two trip times and dividing by the number of trips: (5 + 12) / 2 = 8.5.

Example 2:

Input: ["UndergroundSystem", "checkIn", "checkIn", "checkOut", "getAverageTime", "checkOut", "getAverageTime"]

[[], [1, "North", 5], [2, "South", 10], [1, "East", 15], ["North", "East"], [2, "East", 30], ["South", "East"]]

Output: [null, null, null, null, 10.00000, null, 20.00000]

Explanation: Two different customers are traveling simultaneously on distinct routes. Customer 1 travels from "North" to "East", finishing their trip in exactly 10 units of time (15 - 5). Checking the average for "North" to "East" returns 10.0. Meanwhile, customer 2 travels from "South" to "East", completing their journey in 20 units of time (30 - 10). A subsequent query for the "South" to "East" route correctly ignores the "North" trips and returns 20.0.

Now Your Turn!

Pick the correct output for the given input

Input: ["UndergroundSystem", "checkIn", "checkOut", "getAverageTime", "checkIn", "checkOut", "getAverageTime"]

[[], [99, "Home", 10], [99, "Work", 40], ["Home", "Work"], [99, "Home", 50], [99, "Work", 70], ["Home", "Work"]]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= id, t <= 106
  • 1 <= stationName.length, startStation.length, endStation.length <= 10
  • All strings consist of uppercase and lowercase English letters and digits.
  • There will be at most 2 * 104 calls in total to checkIn, checkOut, and getAverageTime.
  • Answers within 10-5 of the actual value will be accepted.

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class UndergroundSystem {
public:
UndergroundSystem() {
}
void checkIn(int id, string stationName, int t) {
}
void checkOut(int id, string stationName, int t) {
}
double getAverageTime(string startStation, string endStation) {
}
};
Test Case

Input:

Methods
Params