Problem · Hash Table

Toll Journey Fares

Learn this problem
MediumOnePay logoOnePayFULLTIMEPHONE SCREEN

Problem statement

You are given a chronological array of highway toll records logs. Each record has four whitespace-separated fields:

timestamp licensePlate boothId eventType

The eventType is ENTRY, MAINROAD, or EXIT. Records for different license plates may be interleaved.

For one car, a complete journey begins with ENTRY, contains zero or more MAINROAD events, and ends with EXIT.

The array tollRates supplies directed segment prices. Each rate record has three whitespace-separated fields:

fromBooth toBooth fareCents

For this exercise, assume a completed journey costs the sum of the fares for every consecutive directed booth pair from its ENTRY through its EXIT.

An ENTRY starts a new candidate journey for that plate and replaces any unfinished candidate. A MAINROAD event extends an active candidate and is ignored otherwise. An EXIT completes and closes an active candidate and is ignored otherwise. Unfinished candidates produce no fare.

Return one fare in cents for every completed journey, ordered by the EXIT records that complete the journeys.

Function

calculateJourneyFares(logs: String[], tollRates: String[]) → long[]

Examples

Example 1

logs = ["0.000 CAR1 A ENTRY","1.000 CAR2 B ENTRY","2.000 CAR1 C MAINROAD","3.000 CAR2 D EXIT","4.000 CAR1 D EXIT","5.000 CAR1 A ENTRY","6.000 CAR1 C EXIT"]tollRates = ["A C 125","C D 200","B D 90"]return = [90,325,125]

CAR2 completes first with fare 90. The first CAR1 journey costs 125 + 200 = 325, and its second journey costs 125.

Example 2

logs = ["0.000 CAR1 A MAINROAD","1.000 CAR1 B EXIT","2.000 CAR1 A ENTRY","3.000 CAR1 B MAINROAD","4.000 CAR1 C ENTRY","5.000 CAR1 D EXIT","6.000 CAR2 E ENTRY"]tollRates = ["A B 50","C D 70"]return = [70]

The stray records are ignored. The second ENTRY for CAR1 discards its earlier unfinished candidate, so only the direct C-to-D journey completes. CAR2 never exits.

Example 3

logs = ["0.000 X A ENTRY","1.000 X B EXIT","2.000 Y B ENTRY","3.000 Y A EXIT"]tollRates = ["A B 40","B A 75"]return = [40,75]

A direct ENTRY-to-EXIT journey has one priced segment. Rates are directed, so A-to-B costs 40 while B-to-A costs 75.

Constraints

  • 0 <= logs.length <= 200000
  • 0 <= tollRates.length <= 200000
  • Every log contains exactly four non-empty whitespace-separated fields and uses event type ENTRY, MAINROAD, or EXIT.
  • The first log field is a timestamp, and logs is in chronological order.
  • Every rate contains exactly two booth IDs followed by an integer fareCents with 0 <= fareCents <= 10^9.
  • Each directed booth pair appears at most once in tollRates, and every segment used by an active candidate has a supplied rate.
  • Every completed journey fare fits in a signed 64-bit integer.

More OnePay problems

drafts saved locally
public long[] calculateJourneyFares(String[] logs, String[] tollRates) {
    // Write your code here.
}
logs["0.000 CAR1 A ENTRY","1.000 CAR2 B ENTRY","2.000 CAR1 C MAINROAD","3.000 CAR2 D EXIT","4.000 CAR1 D EXIT","5.000 CAR1 A ENTRY","6.000 CAR1 C EXIT"]
tollRates["A C 125","C D 200","B D 90"]
expected[90,325,125]
checking account