Karat / VO Coding Interview OA: Log File Log Parsing and Speeding Detection

19 Views
No Comments

We are writing software to analyze logs for toll booths on a highway. This highway is a divided highway with limited access; the only way on to or off of the highway is through a toll booth.

There are three types of toll booths:

  • ENTRY (E in the diagram) toll booths, where a car goes through a booth as it enters the highway.
  • EXIT (X in the diagram) toll booths, where a car goes through a booth as it exits the highway.
  • MAINROAD (M in the diagram), which have sensors that record a license plate as a car drives through at full speed.

We would like to catch people who are driving at unsafe speeds on the highway. To help us do that, we would like to identify journeys where a driver does either of the following:

  • Drive an average of 130 km/h or greater in any individual 10 km segment of tollway.
  • Drive an average of 120 km/h or greater in any two 10 km segments of tollway.

For example, consider the following journey:

1000.000 TST002 270W ENTRY
1275.000 TST002 260W EXIT

In this case, the driver of TST002 drove 10 km in 275 seconds. We can calculate that this driver drove an average speed of about 130.91 km/h over this segment:

10 km * 3600 sec/hr
-------------------- = 130.91 km/hr
      275 sec

Note that:

  • A license plate may have multiple journeys in one file, and if they drive at unsafe speeds in both journeys, both should be counted.
  • We do not mark speeding if they are not on the highway (i.e. for any driving between an EXIT and ENTRY event).
  • Speeding is only marked once per journey. For example, if there are 4 segments at 120 km/h or greater, or multiple segments at 130 km/h or greater, the journey is only counted once.

Write a function catch_speeders in LogFile that returns a collection of license plates that drove at unsafe speeds during a journey in the LogFile. If the same license plate drives at unsafe speeds during two different journeys, the license plate should appear twice (once for each journey they drove at unsafe speeds).

For our first task:

  • Read through and understand the code and comments below. Feel free to run the code and tests.
  • The tests are not passing due to a bug in the code. Make the necessary changes to LogEntry to fix the bug.

This problem is about parsing toll-booth logs and detecting speeding journeys. Each log line contains a timestamp, a license plate, a booth location plus direction, and a booth type, so the first step is to correctly parse fields such as location-direction tokens like <code>210E</code> into numeric location and direction. Then the log data must be analyzed by license plate and journey order to determine when a vehicle is actually on the highway and whether it exceeds either speeding rule: 130 km/h or more in a single 10 km segment, or 120 km/h or more across any two 10 km segments. A correct solution typically groups events by plate, tracks each journey separately, and counts multiple unsafe journeys independently. The visible bug in <code>LogEntry</code> is in the timestamp parsing, which should store a numeric value rather than a string.

END
 0