FastPrepDesign Underground System

Design Underground System

Bloomberg LP logoBloomberg LP● MediumNEW GRADPHONE SCREENONSITE INTERVIEW
Learn

Problem statement

Process operations for an underground transit system.

  • ["checkIn", id, station, time] records that passenger id entered station at integer time.
  • ["checkOut", id, station, time] completes that passenger's active trip.
  • ["getAverageTime", start, end] asks for the average duration of every completed trip from start to end.

Return the answers to the average-time operations in encounter order. Trips in the reverse direction belong to a different route.

Function

undergroundSystem(operations: String[][]) → double[]

Examples

Example 1

operations = [["checkIn","45","Leyton","3"],["checkIn","32","Paradise","8"],["checkOut","45","Waterloo","15"],["checkOut","32","Cambridge","22"],["getAverageTime","Paradise","Cambridge"],["getAverageTime","Leyton","Waterloo"]]return = [14.0,12.0]

The completed trips last 22-8=14 and 15-3=12 time units.

Example 2

operations = [["checkIn","1","A","2"],["checkOut","1","B","8"],["checkIn","2","A","10"],["checkOut","2","B","20"],["getAverageTime","A","B"]]return = [8.0]

The two durations are 6 and 10, so their average is 8.

Constraints

  • 1 <= operations.length <= 10^5.
  • Passenger IDs and times are decimal integers encoded as strings.
  • Every check-out has one active matching check-in, and an ID has at most one active trip.
  • Every average-time query has at least one completed matching route.
  • Times are strictly increasing within each passenger's trip.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public double[] undergroundSystem(String[][] operations) {
  // Write your code here.
}
operations[["checkIn","45","Leyton","3"],["checkIn","32","Paradise","8"],["checkOut","45","Waterloo","15"],["checkOut","32","Cambridge","22"],["getAverageTime","Paradise","Cambridge"],["getAverageTime","Leyton","Waterloo"]]
expected[14.0,12.0]
Checking account…