Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top Error Codes from Logs

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

Your question is Top Error Codes from 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

Given an array of log lines logs, where each line is a string, write a function that extracts error codes and returns the 10 most frequent codes. An error code is any token that starts with ERROR_ and is followed by uppercase letters, digits, or underscores. Return a list of (code, count) pairs sorted by descending frequency, and break ties by lexicographically smaller code first.

Constraints

  • 1 <= len(logs) <= 10^5
  • 0 <= len(logs[i]) <= 10^3
  • Each log line may contain zero or more tokens
  • A valid error code matches ERROR_[A-Z0-9_]+
  • Return at most 10 distinct error codes

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