Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top K Vehicle IDs in Logs

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Top K Vehicle IDs in Logs. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Cox Automotive processes vehicle telemetry logs that may be too large to load into memory at once. Given an iterable of log lines and an integer k, return the vehicle IDs that report most frequently.

Each line uses the format timestamp,vehicle_id,event, and the vehicle ID is the second comma-separated field. Return exactly k IDs when at least k distinct vehicles exist. Rank vehicles by descending report count, then lexicographically ascending vehicle ID to make ties deterministic.

Your function must process the input as an iterable, such as a file iterator, rather than requiring the entire log to be stored in a list. Use an algorithm that avoids sorting every distinct vehicle when k is much smaller than the number of vehicles.

Formal Specification

  • Input: log_lines, an iterable of strings, and k, a positive integer.
  • Output: A list of vehicle ID strings ordered by rank.
  • Every line is valid and contains at least two comma-separated fields.

Constraints

  • 1 <= k
  • 0 <= number of log lines <= 10^8
  • 1 <= number of distinct vehicle IDs <= 10^7 when the log is non-empty
  • Vehicle IDs are non-empty strings of at most 64 characters
  • Every line is valid and contains at least two comma-separated fields
  • The result uses descending count, then ascending vehicle ID

Function Signature

def top_k_vehicle_ids(log_lines, k):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output