Problem · Hash Table

Count Complete Toll Journeys

Learn this problem
EasyOnePay 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.

For this exercise, assume 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.

Return the total number of complete journeys across all license plates.

Function

countCompleteJourneys(logs: String[]) → int

Examples

Example 1

logs = ["90750.191 JOX304 250E ENTRY","91081.684 JOX304 260E MAINROAD","91082.101 THX138 110E ENTRY","91483.251 JOX304 270E MAINROAD","91873.920 THX138 120E MAINROAD","91874.493 JOX304 280E EXIT","91982.102 THX138 290E EXIT","92301.302 THX138 300E ENTRY","92371.302 THX138 310E EXIT"]return = 3

JOX304 completes one journey. THX138 completes one journey ending at timestamp 91982.102 and another ending at 92371.302, for a total of 3.

Example 2

logs = ["1.000 CAR1 A MAINROAD","2.000 CAR1 B EXIT","3.000 CAR2 C ENTRY","4.000 CAR2 D MAINROAD"]return = 0

The first two records have no preceding entry for CAR1. The CAR2 entry never reaches an exit, so no complete journey is counted.

Example 3

logs = ["1.000 CAR1 A ENTRY","2.000 CAR1 B ENTRY","3.000 CAR1 C MAINROAD","4.000 CAR1 D EXIT","5.000 CAR1 E EXIT"]return = 1

The second entry replaces the unfinished CAR1 candidate. Its following main-road record and exit complete one journey. The final exit has no active candidate.

Constraints

  • 0 <= logs.length <= 200000
  • Every record contains exactly four non-empty whitespace-separated fields.
  • The first field is a timestamp, and logs is in chronological order.
  • The second and third fields are a license plate and booth ID.
  • The fourth field is ENTRY, MAINROAD, or EXIT.

More OnePay problems

drafts saved locally
public int countCompleteJourneys(String[] logs) {
    // Write your code here.
}
logs["90750.191 JOX304 250E ENTRY","91081.684 JOX304 260E MAINROAD","91082.101 THX138 110E ENTRY","91483.251 JOX304 270E MAINROAD","91873.920 THX138 120E MAINROAD","91874.493 JOX304 280E EXIT","91982.102 THX138 290E EXIT","92301.302 THX138 300E ENTRY","92371.302 THX138 310E EXIT"]
expected3
checking account