Count Complete Highway Journeys from Toll Logs
Company: Onepay
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
# Count Complete Highway Journeys from Toll Logs
Each toll record is `(timestamp, plate, location, booth_type)`. `booth_type` is `ENTRY`, `MAINROAD`, or `EXIT`. The final character of `location` is the travel direction, `E` or `W`; the preceding integer is a mile marker.
Implement `count_complete_journeys(records)` and return the number of valid completed journeys.
A journey for one plate and direction starts with `ENTRY`, may contain zero or more `MAINROAD` records, and ends with `EXIT`. Records may be unsorted; process them by `(timestamp, original_input_index)`. A new `ENTRY` replaces any incomplete journey for the same plate. A `MAINROAD` or `EXIT` with no active journey, or with a direction different from the active journey, is ignored. A valid `EXIT` increments the count and clears that plate's active journey.
## Constraints
- At most `200000` records.
- Timestamps are non-negative numbers.
- Plates and locations are non-empty strings.
- A plate has at most one active journey at a time.
## Example
The records below contain two complete journeys:
```text
(1, "JOX304", "250E", ENTRY)
(2, "THX138", "110E", ENTRY)
(3, "JOX304", "270E", MAINROAD)
(4, "JOX304", "280E", EXIT)
(5, "THX138", "290E", EXIT)
```
## Clarifications
This task counts journeys only; no toll-rate or speed formula is supplied. Explain how state is isolated per plate and how malformed sequences are handled deterministically.
## Hints
After sorting, one small state record per currently active plate is sufficient.
## Extensions
- Compute distance or detect speeding when roadway metadata is provided.
- Process an unbounded stream with bounded state.
- Handle duplicate log delivery idempotently.
Quick Answer: Count completed highway journeys from unsorted entry, main-road, and exit toll records grouped by vehicle and direction. Handle replacement entries, mismatched directions, incomplete or malformed sequences, deterministic tie ordering, duplicate delivery, and bounded-state streaming follow-ups.
Each record is a four-element sequence `(timestamp, plate, location, booth_type)`. The entire list of records is passed as the single argument `records`. `booth_type` is `ENTRY`, `MAINROAD`, or `EXIT`. The final character of each non-empty `location` is the travel direction, `E` or `W`; the preceding integer is a mile marker.
Return the number of valid completed journeys.
Process records in ascending `(timestamp, original_input_index)` order, so records with equal timestamps keep their original order. State is isolated by plate, and a plate has at most one active journey. An `ENTRY` starts a journey in that record's direction and replaces any incomplete journey already active for the same plate. A `MAINROAD` record is accepted only when that plate has an active journey in the same direction; otherwise it is ignored. An `EXIT` completes and counts a journey only when the plate has an active journey in the same direction, then clears that plate's state. An `EXIT` or `MAINROAD` with no active journey is ignored.
A journey may contain zero or more matching `MAINROAD` records. Mile markers do not affect this counting task.
Constraints
- 0 <= records.length <= 200000.
- Each record contains exactly (timestamp, plate, location, booth_type).
- Timestamps are non-negative numbers.
- Plates and locations are non-empty strings.
- Each location consists of an integer mile marker followed by E or W.
- booth_type is ENTRY, MAINROAD, or EXIT.
- A plate has at most one active journey at a time.
Examples
Input: ([(1, 'JOX304', '250E', 'ENTRY'), (2, 'THX138', '110E', 'ENTRY'), (3, 'JOX304', '270E', 'MAINROAD'), (4, 'JOX304', '280E', 'EXIT'), (5, 'THX138', '290E', 'EXIT')],)
Expected Output: 2
Explanation: The exact source example completes JOX304 after one MAINROAD record and THX138 with no MAINROAD record.
Input: ([],)
Expected Output: 0
Explanation: With no records, no plate can start or complete a journey.
Hints
- Attach each record's original input index before sorting so equal timestamps remain deterministic.
- Keep only the active direction for each plate; mile markers are not needed to count journeys.
- Handle ENTRY replacement separately from MAINROAD and EXIT, which act only on a matching active direction.