Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Parse Logs for Frequent Errors
00:00
5 left

Parse Logs for Frequent Errors

MediumPython

Problem

Given a large log file, write a function to parse it and return the most frequent error messages.

Accept log_lines as an iterable of strings in the format LEVEL|message, and count only ERROR entries. Return the top k messages as [message, count] pairs, ordered by descending count and then ascending message text.

Constraints

  • 1 <= number of log lines <= 9999
  • Each line contains at most 1000 characters
  • A valid line has the form LEVEL|message
  • 1 <= k <= 1000
  • Only lines with level ERROR are counted
  • Return at most k results

Function Signature

def most_frequent_errors(log_lines, k):
Interviewer

Your question is Parse Logs for Frequent Errors. Start with the requirements in the Question tab.

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.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.