Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top Five Error Codes

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

Your question is Top Five 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

Given a list of log lines logs, write a function that extracts error codes and returns the five most frequent ones. An error code appears only in lines containing the token ERROR and is the first alphanumeric token immediately following ERROR. Return a list of [code, count] pairs sorted by descending frequency, and for ties sort lexicographically by code.

Constraints

  • 1 <= len(logs) <= 10^5
  • 1 <= len(logs[i]) <= 500
  • Error codes are non-empty alphanumeric tokens immediately following ERROR
  • If fewer than five distinct error codes exist, return all of them

Function Signature

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