Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top 10 Frequent Error Codes

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

Your question is Top 10 Frequent Error Codes. 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

Affirm services emit pipe-delimited log records. Given a stream of valid log strings, identify the 10 most frequent error codes without storing every log record.

Each log has the format timestamp|level|error_code|message. The error code is the third field, at index 2. Count every record, including repeated codes. If fewer than 10 distinct error codes appear, return all of them.

Return a list of objects ordered by descending frequency. When two codes have the same frequency, order them lexicographically by error code. Each object must contain code and count.

Formal Specification

Implement top_error_codes(logs), where logs is an iterable of strings. Return a list of dictionaries with the shape {"code": string, "count": integer}.

The input iterable may be consumed only once. Do not sort all distinct codes to select the result unless using the alternative approach described in your explanation.

Constraints

  • 0 <= len(logs) <= 10^6
  • Each record contains at least four pipe-delimited fields
  • Each error code is a non-empty string of at most 64 characters
  • There may be up to 10^5 distinct error codes
  • The result contains at most 10 entries

Function Signature

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