Count Trips From Vehicle Logs
Company: LinkedIn
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
Quick Answer: This question evaluates the ability to process sequential event logs and reason about per-entity state transitions, exercising skills in data structures, event-stream processing, and correctness under interleaved records.
Constraints
- 0 <= len(logs) <= 200000
- Each log record is a tuple `(license_plate, event_type)`
- `license_plate` is a non-empty string
- `event_type` is one of: `ENTRY`, `ROAD`, `EXIT`
Examples
Input: ([('A123', 'ENTRY'), ('B999', 'ENTRY'), ('A123', 'ROAD'), ('A123', 'EXIT'), ('B999', 'ROAD'), ('B999', 'EXIT'), ('A123', 'ENTRY'), ('A123', 'EXIT')],)
Expected Output: 2
Explanation: A123 completes one valid trip and B999 completes one valid trip. The final A123 sequence is ENTRY -> EXIT, so it does not count.
Input: ([],)
Expected Output: 0
Explanation: No logs means no completed trips.
Input: ([('X1', 'EXIT'), ('X1', 'ENTRY'), ('X1', 'ENTRY'), ('X1', 'ROAD'), ('X1', 'EXIT'), ('X1', 'EXIT')],)
Expected Output: 1
Explanation: The first EXIT is invalid. Duplicate ENTRY restarts the candidate trip. ENTRY -> ROAD -> EXIT forms exactly one valid trip. The final EXIT is invalid.
Input: ([('A', 'ENTRY'), ('A', 'ROAD'), ('B', 'ENTRY'), ('A', 'EXIT'), ('A', 'ENTRY'), ('B', 'ROAD'), ('A', 'ROAD'), ('B', 'EXIT'), ('A', 'EXIT')],)
Expected Output: 3
Explanation: A completes one trip before B finishes, then B completes one trip, then A completes a second trip. Interleaving does not matter because each plate is tracked separately.
Input: ([('C', 'ENTRY'), ('C', 'ROAD'), ('C', 'ENTRY'), ('C', 'EXIT'), ('C', 'ENTRY'), ('C', 'ROAD'), ('C', 'EXIT')],)
Expected Output: 1
Explanation: The second ENTRY replaces the earlier incomplete trip, so the following EXIT does not count. The final ENTRY -> ROAD -> EXIT sequence counts as one trip.
Input: ([('D', 'ENTRY'), ('D', 'ROAD'), ('D', 'ROAD'), ('E', 'ROAD'), ('D', 'EXIT'), ('E', 'ENTRY'), ('E', 'EXIT')],)
Expected Output: 1
Explanation: Extra ROAD for D does not create an extra trip; D still completes exactly one. E has ROAD before ENTRY and then ENTRY -> EXIT without ROAD, so E contributes zero.
Hints
- Treat each license plate independently, even though their events are interleaved in one shared log.
- A small state machine per vehicle is enough: no active trip, seen ENTRY, or seen ENTRY and ROAD.