Inference Log Summary
Description
You are given raw log lines from a model-serving system. Each line describes one inference request:
timestamp|model_id|status|latency_msWrite summarize_logs(lines) that returns, for each model, how many requests it served, how many succeeded, and its average latency.
{
model_id: {
"total_requests": int,
"success_requests": int,
"avg_latency_ms": float, # rounded to 2 decimals
}
}Logs are messy. Skip a line entirely when any of these is true:
- it does not have exactly 4 fields
timestamp,statusorlatency_msis not an integerlatency_msis negativemodel_idis empty after stripping whitespace
Strip surrounding whitespace from model_id, so " model-a " and "model-a" are the same model. A request succeeded when 200 <= status < 300. Models with no valid lines do not appear in the result.
Example
summarize_logs([
"1000|llama-3|200|120",
"1001|mistral|500|80",
"1002|llama-3|201|160",
])
# {
# "llama-3": {"total_requests": 2, "success_requests": 2, "avg_latency_ms": 140.0},
# "mistral": {"total_requests": 1, "success_requests": 0, "avg_latency_ms": 80.0},
# }Constraints
0 <= len(lines) <= 10^5- One pass over the input is enough. Don't store the lines.
Solution
Reference solution and explanationTry it first. The explanation covers the approach, complexity, pitfalls and follow-ups.Reveal solution
Approach
Make one pass and keep three running numbers per model: request count, success count and latency sum. Validate each line before touching the totals, so a bad line can never leave a model half-counted. Compute the averages at the end.
Validate in the cheap order: field count first, then the blank model id, then the integer conversions inside one try. Putting all three int() calls in one block means any bad field skips the whole line.
Complexity
O(n) time over the lines, O(m) space for m distinct models. The lines are never stored.
Pitfalls
- Checking
latency_ms < 0after you have already added the line to the totals. - Forgetting to strip
model_id, which splits one model into two keys. - Using
status == 200instead of the200 <= status < 300range. - Dividing inside the loop. Keep sums and divide once at the end.
- Parsing with
float()instead ofint(). Then12.5or1e3passes as a valid integer field.
Follow-ups interviewers ask
- Add p50 and p99 latency. Exact percentiles need every value, or a sorted structure per model. At scale you'd use a sketch such as t-digest or a fixed histogram.
- The log is 200 GB. Stream it line by line from disk, and shard by
model_idif one machine is too slow. - Report why lines were rejected, as counts per reason, so a bad deploy shows up in the summary.
Reference solution
from collections import defaultdict
def summarize_logs(lines: list[str]) -> dict:
totals = defaultdict(lambda: [0, 0, 0]) # requests, successes, latency sum
for line in lines:
parts = line.split("|")
if len(parts) != 4:
continue
model_id = parts[1].strip()
if not model_id:
continue
try:
int(parts[0])
status = int(parts[2])
latency_ms = int(parts[3])
except ValueError:
continue
if latency_ms < 0:
continue
stats = totals[model_id]
stats[0] += 1
stats[2] += latency_ms
if 200 <= status < 300:
stats[1] += 1
return {
model_id: {
"total_requests": n,
"success_requests": ok,
"avg_latency_ms": round(latency / n, 2),
}
for model_id, (n, ok, latency) in totals.items()
}Notes
def summarize_logs(lines: list[str]) -> dict:
"""
Parse inference request logs.
Format: timestamp|model_id|status|latency_ms
Return {model_id: {"total_requests": int, "success_requests": int, "avg_latency_ms": float}}.
Skip invalid lines.
"""
raise NotImplementedErrorRun checks the 2 example tests. Submit runs all 11 and marks the problem solved when they pass.