Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Trending in AI
Hot
Agentic AI EngineerForward-Deployed EngineerGenAI EngineerAI Architect
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
Start free
Process a Large File Line by Line
00:00
5 left

Process a Large File Line by Line

EasyPython

Problem

Fractal analytics pipelines may receive log files that are too large to load into memory. Write a function that processes an iterable of log lines one at a time and summarizes lines containing a target keyword.

A line matches when the keyword appears as a case-sensitive substring. Return the total number of matching lines and the zero-based line numbers of the first three matches. The input may be a file object, generator, or list, so your solution must not convert the entire iterable to a list.

Formal Specification

Implement process_log_lines(lines, keyword).

  • lines is an iterable of strings. Each string represents one line and may include a trailing newline character.
  • keyword is a non-empty string.
  • Return a dictionary with this exact structure: {"matching_count": integer, "first_matches": list of integers}.
  • first_matches contains at most three zero-based line numbers, in ascending order.

Constraints

  • 1 <= number of lines <= 10^7
  • 1 <= len(keyword) <= 100
  • Each line contains at most 10^6 characters
  • The iterable may be single-use and should be consumed only once

Function Signature

def process_log_lines(lines, keyword):
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements are in the Question tab.

Sign up free to run or submit. Log in
Code
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 2
Run your code to see test output here.