Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Get started
Parse Logs or Tools Like ps
00:00
5 left

Parse Logs or Tools Like ps

MediumPython

Problem

Scripting: implement a simplified Linux tool such as ps or find, or parse logs.

Asked in the Scripting stage. Implement parse_logs(lines, level) to process structured log lines.

Each line has the form timestamp level process message, with fields separated by single spaces. Return a list of [process, count] pairs for lines whose level equals level. Sort by descending count, then ascending process name. All input lines are valid, and the message may contain spaces.

Example 1: lines = ["10:00 INFO worker started", "10:01 ERROR api failed", "10:02 ERROR worker retrying"], level = "ERROR" returns [["api", 1], ["worker", 1]].

Example 2: lines = ["10:00 WARN api slow", "10:01 WARN api retry", "10:02 INFO worker done"], level = "WARN" returns [["api", 2]].

Constraints

  • 1 <= lines.length <= 5000
  • Each line has the form timestamp level process message
  • Fields are separated by single spaces
  • The message is non-empty and may contain spaces
  • The requested level is a non-empty string
  • All input lines are valid

Function Signature

def parse_logs(lines, level):
Interviewer

Your question is Parse Logs or Tools Like ps. 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
Sign up free to run your codeLog inLn 2
Run your code to see test output here.