Implement Journey Counter and Speeder Detector
Problem Description
You are tasked with processing highway toll log data to identify complete journeys and detect speeding vehicles. The input is a log file, which can be parsed using provided LogEntry and LogFile classes.
Each line in the log file follows this format:
timestamp license_plate location booth_type
Example Log Line:
44776.619 KTB918 310E MAINROAD
Field Definitions:
timestamp: A floating-point number representing time in seconds.license_plate: A string identifier for the vehicle (e.g.,KTB918).location: An integer followed by a direction letter (e.g.,310E,400W).booth_type: One ofENTRY,MAINROAD, orEXIT.
Journey Definition: A complete journey is defined by a sequence of log entries for a single vehicle:
- One
ENTRYrecord. - Followed by zero or more
MAINROADrecords. - Followed by one
EXITrecord.
Assumptions:
- The log contains only complete journeys.
- There are no missing records within a journey.
- The input log entries are provided in chronological order.
Implementation Tasks
You need to implement or fix the following functionalities:
1. Parse Log Entries Correctly
Modify the LogEntry.__init__(log_line) method to correctly parse the input log line:
- Parse
timestampas a numeric (floating-point) value, not a string. - Parse
locationinto two distinct attributes:location: The integer part of the location (e.g.,310from310E).direction: The direction part, which should beEASTif the suffix isE, orWESTif the suffix isW.
2. Implement count_journeys()
This method should return the total number of complete journeys found in the entire log file.
3. Implement catch_speeders()
This method should return a collection (e.g., a list or set) of license plates that have committed speeding violations.
Speeding Rules:
- Each pair of consecutive checkpoints (log entries) within the same journey represents a
10 kmsegment. - Segment speed is calculated as:
speed_kmh = (10 * 3600) / time_diff_seconds. - A journey is considered speeding if either of the following conditions is met:
- Any single
10 kmsegment within that journey has a speed of at least130 km/h. - At least two distinct
10 kmsegments within the same journey have a speed of at least120 km/h.
- Any single
Additional Rules for catch_speeders():
- A license plate may appear multiple times in the result if it has multiple distinct speeding journeys.
- Count each speeding journey only once, even if it contains more than two speeding segments.
- Do not compute speeds across different journeys for the same car; specifically, an
EXITfrom one journey should not be connected to a laterENTRYfor speed calculation.
AI interview prep
Core dilemma
Efficiently processing sequential, grouped data to identify complex patterns and perform calculations, specifically managing per-vehicle, per-journey state to detect speeding violations while adhering to strict rules about journey boundaries and segment definitions. This involves balancing parsing robustness, data structure efficiency, and algorithmic complexity for potentially large datasets.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement Journey Counter and Speeder Detector
Problem Description
You are tasked with processing highway toll log data to identify complete journeys and detect speeding vehicles. The input is a log file, which can be parsed using provided LogEntry and LogFile classes.
Each line in the log file follows this format:
timestamp license_plate location booth_type
Example Log Line:
44776.619 KTB918 310E MAINROAD
Field Definitions:
timestamp: A floating-point number representing time in seconds.license_plate: A string identifier for the vehicle (e.g.,KTB918).location: An integer followed by a direction letter (e.g.,310E,400W).booth_type: One ofENTRY,MAINROAD, orEXIT.
Journey Definition: A complete journey is defined by a sequence of log entries for a single vehicle:
- One
ENTRYrecord. - Followed by zero or more
MAINROADrecords. - Followed by one
EXITrecord.
Assumptions:
- The log contains only complete journeys.
- There are no missing records within a journey.
- The input log entries are provided in chronological order.
Implementation Tasks
You need to implement or fix the following functionalities:
1. Parse Log Entries Correctly
Modify the LogEntry.__init__(log_line) method to correctly parse the input log line:
- Parse
timestampas a numeric (floating-point) value, not a string. - Parse
locationinto two distinct attributes:location: The integer part of the location (e.g.,310from310E).direction: The direction part, which should beEASTif the suffix isE, orWESTif the suffix isW.
2. Implement count_journeys()
This method should return the total number of complete journeys found in the entire log file.
3. Implement catch_speeders()
This method should return a collection (e.g., a list or set) of license plates that have committed speeding violations.
Speeding Rules:
- Each pair of consecutive checkpoints (log entries) within the same journey represents a
10 kmsegment. - Segment speed is calculated as:
speed_kmh = (10 * 3600) / time_diff_seconds. - A journey is considered speeding if either of the following conditions is met:
- Any single
10 kmsegment within that journey has a speed of at least130 km/h. - At least two distinct
10 kmsegments within the same journey have a speed of at least120 km/h.
- Any single
Additional Rules for catch_speeders():
- A license plate may appear multiple times in the result if it has multiple distinct speeding journeys.
- Count each speeding journey only once, even if it contains more than two speeding segments.
- Do not compute speeds across different journeys for the same car; specifically, an
EXITfrom one journey should not be connected to a laterENTRYfor speed calculation.