Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top Five Frequent Error Logs

MediumPython00:00
I
Practice interviewer
Your interviewer
In session
I
Interviewer

Welcome to the Python screen.

The question is on your right: Top Five Frequent Error Logs. Read through the requirements first.

Run and submit your code as often as you need. You also have five interviewer messages this session - want to talk through your approach, or are you ready to start coding?

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

Problem

In Meta infrastructure systems, engineers often need to summarize large volumes of Scuba-style server logs. Given a list of log lines, return the top 5 most frequent error messages.

A log line is considered an error message only if it starts with the prefix "ERROR: ". The message is the substring after that prefix. Count identical messages together and return up to 5 messages ordered by descending frequency. If two messages have the same frequency, return them in lexicographically ascending order.

Formal Specification

Implement a function that takes a list of strings logs and returns a list of strings.

  • Input: logs, a list of log lines
  • Output: a list containing up to 5 error messages

Ignore all non-error log lines.

Constraints

  • 1 <= len(logs) <= 10^5
  • 0 <= len(logs[i]) <= 200
  • Each log line is an ASCII string
  • A valid error line starts exactly with "ERROR: "
  • Return at most 5 messages
  • If fewer than 5 distinct error messages exist, return all of them

Function Signature

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