← Questions

Parse and summarize HTTP access logs

0
AppleCodingLast asked May 2026

A coding interview question asked in Apple's technical phone screen, last asked May 2026. It tests parsing, strings and logs.

You get a list of raw access-log lines. Each valid line holds an IP address, a timestamp, a request path and a status code. Parse the lines, reject the malformed ones, and report how many requests there were, how many failed with a client or a server error, and the failure percentage.

Each line has the form <ip> <timestamp> <path> <status>, with fields separated by one or more spaces:

  • ip: an IPv4 or IPv6 address that a standard address parser accepts.
  • timestamp: an RFC 3339 timestamp with no spaces, such as 2026-03-01T10:00:00Z.
  • path: starts with / and has no spaces.
  • status: exactly three digits, from 100 to 599.

A line is valid only if it has exactly four fields and every field passes its check.

Part 1: parse one line

  • parse_line(line: str) -> Record | ParseError — returns a typed record, or an error saying which field failed. It never guesses a missing field and never accepts a status like 500oops because it starts with digits.

Part 2: summarize

  • summarize(lines: list[str]) -> dict — returns total_requests, client_errors (4xx), server_errors (5xx), failed_requests (4xx + 5xx), failure_percent and invalid_lines.
  • Invalid lines count only in invalid_lines. They are left out of every other count and out of the percentage.
  • failure_percent = 100 * failed_requests / total_requests, and 0 when there are no valid requests.

Example

10.0.0.1 2026-03-01T10:00:00Z /api/users 200     valid
10.0.0.2 2026-03-01T10:00:01Z /api/orders 404    valid, client error
2001:db8::7 2026-03-01T10:00:02Z /checkout 503   valid, server error
10.0.0.3 2026-03-01T10:00:03Z /login 500oops     invalid status
999.1.1.1 2026-03-01T10:00:04Z /home 200         invalid IP
10.0.0.4 2026-03-01T10:00:05Z /home              invalid: three fields

-> total_requests 3, client_errors 1, server_errors 1, failed_requests 2,
   failure_percent 66.67, invalid_lines 3

Part 3: test and stream

Write the tests you would ship, and change the code so it works on a stream of lines that doesn't fit in memory.

Clarifying questions

  • Are timestamps always in UTC, or can they carry other offsets?
  • Can a path be quoted or contain spaces?
  • Should the percentage be rounded, and where: in the result or only when printed?
  • Do you want to know why each line was rejected, or only how many were?

What a strong answer covers

  • Check the field count before reading fields; use the standard library to validate IP addresses and timestamps; match the whole status token, then check its range.
  • Keep parsing separate from counting, with one documented denominator and a defined result for zero valid lines.
  • Boundary tests at 399, 400, 499, 500 and 599, plus missing, extra and malformed fields and both IPv4 and IPv6.
  • One linear pass with constant state, so the same code works on a stream.

Follow-ups

  • How would quoted fields or paths with spaces change the parser?
  • A deploy upstream changes the log format and invalid lines spike. Which metric shows it, and how do you cap the diagnostics you log?
  • How do you report rejected lines without logging sensitive request data?

Where it's asked

  • Apple · Technical phone screen · Last asked May 2026

What it tests

parsing · strings · logs

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding