Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Process a Large File Line by Line

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

Your question is Process a Large File Line by Line. 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

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):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output