Average Travel Times for Directed Station Pairs

Quick Overview

Average Travel Times for Directed Station Pairs. Implement UndergroundSystem with its native constructor and methods.

Average Travel Times for Directed Station Pairs

Company: Bloomberg

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Implement UndergroundSystem with its native constructor and methods. checkIn records a customer's station and time; checkOut completes that customer's direct trip. getAverageTime returns the average duration of all completed direct trips from startStation to endStation. Reverse-direction trips are separate. Calls are consistent, each checkout follows its matching checkin at a strictly later time, events are chronological,and every queried route has at least one completed trip. ```cpp 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); }; ``` ### Examples **Example 1:** ```text Calls: UndergroundSystem(); checkIn(1,"Oak",1); checkOut(1,"Pine",7); getAverageTime("Oak","Pine"); checkIn(2,"Oak",8); checkOut(2,"Pine",16); getAverageTime("Oak","Pine") Returned query values: [6.0,7.0] Explanation: Completed durations are6 and8, so the averages are6 and(6+8)/2=7. ``` **Example 2:** ```text Calls: UndergroundSystem(); checkIn(1,"A",1); checkOut(1,"B",5); checkIn(2,"B",6); checkOut(2,"A",15); getAverageTime("A","B"); getAverageTime("B","A") Returned query values: [4.0,9.0] Explanation: The two directed routes retain separate totals. ``` ### Constraints - `1 <= id,t <= 1000000`. - Station names and query endpoint names have length 1 to 10 and use uppercase/lowercase English letters and digits. - At most 20000 calls in total to checkIn,checkOut,and getAverageTime. - Calls are consistent and chronological; a customer has at most one active checkin, checkout time is later,and each queried directed route has a completed trip. - Answers within 10^-5 of the true value are accepted. ### Contract considerations Use the constructor and methods shown above. `getAverageTime` returns a `double`; answers within `10^-5` of the true route average are accepted. ```hint Read the boundary condition Check repeated trips, reused customer IDs, and opposite directions between the same stations. Only completed direct trips contribute to a query. ``` ### Boundary reminders Use the constructor and methods shown above. `getAverageTime` returns a `double`; answers within `10^-5` of the true route average are accepted.

Overview: Average Travel Times for Directed Station Pairs. Implement UndergroundSystem with its native constructor and methods.

Bloomberg logo
Bloomberg
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Implement UndergroundSystem with its native constructor and methods. checkIn records a customer's station and time; checkOut completes that customer's direct trip. getAverageTime returns the average duration of all completed direct trips from startStation to endStation. Reverse-direction trips are separate. Calls are consistent, each checkout follows its matching checkin at a strictly later time, events are chronological,and every queried route has at least one completed trip.

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);
};

Examples

Example 1:

Calls: UndergroundSystem(); checkIn(1,"Oak",1); checkOut(1,"Pine",7); getAverageTime("Oak","Pine"); checkIn(2,"Oak",8); checkOut(2,"Pine",16); getAverageTime("Oak","Pine")
Returned query values: [6.0,7.0]
Explanation: Completed durations are6 and8, so the averages are6 and(6+8)/2=7.

Example 2:

Calls: UndergroundSystem(); checkIn(1,"A",1); checkOut(1,"B",5); checkIn(2,"B",6); checkOut(2,"A",15); getAverageTime("A","B"); getAverageTime("B","A")
Returned query values: [4.0,9.0]
Explanation: The two directed routes retain separate totals.

Constraints

  • 1 <= id,t <= 1000000 .
  • Station names and query endpoint names have length 1 to 10 and use uppercase/lowercase English letters and digits.
  • At most 20000 calls in total to checkIn,checkOut,and getAverageTime.
  • Calls are consistent and chronological; a customer has at most one active checkin, checkout time is later,and each queried directed route has a completed trip.
  • Answers within 10^-5 of the true value are accepted.

Contract considerations

Use the constructor and methods shown above. getAverageTime returns a double; answers within 10^-5 of the true route average are accepted.

Boundary reminders

Use the constructor and methods shown above. getAverageTime returns a double; answers within 10^-5 of the true route average are accepted.