Python coding · Build it
A generator pipeline over a 20 GB log file
Practise this out loud
Say your answer out loud to an AI interviewer that follows up, and get a score for it.
Practise by voiceThe 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
- Where does memory still grow in your solution?
- The file is gzipped. What changes?
- Some lines are broken. Skip, count, or stop?
- It takes 20 minutes. How would you make it faster?
- 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 withmaxsplit, yield a small record, count and skip malformed lines.errors_only(records): keep 500–599.- Aggregate in one dict
path -> [count, total_ms], thenheapq.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
splitworks; 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 voicePractise 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.