Problem · Hash Table

Count Completed Trips

Learn this problem
EasyLinkedIn logoLinkedInFULLTIMEPHONE SCREEN

Problem statement

You are given vehicle event logs in chronological order. Each log contains a license plate and one of three event types: entry, road, or exit.

A completed trip is counted when the same plate appears in order as entry -> road -> exit.

Count the total number of completed trips across all plates.

Function

countCompletedTrips(logs: String[]) → int

Complete the function countCompletedTrips in the editor below.

countCompletedTrips has the following parameter:

  1. String[] logs: log lines formatted as "plate event"

Returns

int: the number of completed trips.

Examples

Example 1

logs = ["A entry", "A road", "A exit"]return = 1

Plate A completes one valid entry -> road -> exit sequence.

Example 2

logs = ["A entry", "B entry", "A road", "A exit", "B exit"]return = 1

Plate A completes one trip. Plate B does not, because it never reaches the road event before exiting.

Constraints

  • Logs are processed in chronological order.
  • The same plate may complete multiple trips.
  • Incomplete or invalid event sequences should not be counted.

More LinkedIn problems

drafts saved locally
public int countCompletedTrips(String[] logs) {
    // write your code here
}
logs["A entry", "A road", "A exit"]
expected1
checking account