Skip to the page

Python coding · Build it

A generator pipeline over a 20 GB log file

The interviewer asks

We have a 20 GB access log, one request per line: time, path, status and response time in milliseconds. Write Python that finds the 10 paths with the most 5xx errors, with their average response time, in constant memory. Use a generator for each step.

Then

Follow-ups the interviewer asks next

  1. Where does memory still grow in your solution?
  2. The file is gzipped. What changes?
  3. Some lines are broken. Skip, count, or stop?
  4. It takes 20 minutes. How would you make it faster?
  5. How would you test each step?

The answer

What a strong answer covers

  • read_lines(path): open(path, encoding="utf-8", errors="replace") and yield each line (file iteration is buffered and lazy).
  • parse(lines): split with maxsplit, yield a small record, count and skip malformed lines.
  • errors_only(records): keep 500–599.
  • Aggregate in one dict path -> [count, total_ms], then heapq.nlargest(10, ...).
  • Memory grows with the number of distinct paths, not lines: normalise ids (/orders/123 → /orders/{id}).
  • Gzip: gzip.open(path, "rt", encoding="utf-8"); nothing else changes.
  • Faster: avoid regex if split works; split the file into byte ranges across processes; or let DuckDB or Polars scan it.
  • Each step takes and yields iterables, so tests feed a short list of lines.

The red pen

Common traps

Avoid:f.readlines() or f.read(); a Counter of every distinct raw URL.

Practise this out loud

Say your answer out loud to an AI interviewer that follows up, and get a score for it.

Practise by voice

Practise questions like this out loud

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview