Parse structured text line by line. Filter entries by level, prefix, or pattern. Extract fields. Count and rank. This is the most common class of Python interview problem.
Problems: 10
Difficulty: Medium – Hard
Key libs: json · csv · collections · urllib.parse
Pattern to recognise: These problems always follow the same pipeline —
read lines → strip/skip blanks → parse format → filter by condition → extract field → aggregate → sort/rank.
Master this pipeline and you can solve any log parsing question in an interview.
P 01Coding ProblemMedium
JSON Lines Log Parser — Top-N Error Hosts
Given a list of strings in JSON-lines format, each entry has level, fields.message, and fields.url.
Find all ERROR entries whose message starts with "Not a redirect error:".
Extract the hostname from the URL, count errors per hostname, and return the top-N as (hostname, count) tuples sorted by count descending.
Example
Input lines (JSON):
{"level":"ERROR","fields":{"message":"Not a redirect error: timeout","url":"https://api.example.com/v1"}}
{"level":"ERROR","fields":{"message":"Not a redirect error: conn","url":"https://api.example.com/v2"}}
{"level":"INFO","fields":{"message":"OK","url":"https://other.com/"}}
{"level":"ERROR","fields":{"message":"Not a redirect error: reset","url":"https://db.internal.io/"}}
find_error_hosts(lines, top_n=2)
→ [("api.example.com", 2), ("db.internal.io", 1)]
json.loadsdefaultdicturlparsestr.startswithsorted + lambdatry/exceptdict.get with default[:top_n] slice
Solution
import json
from urllib.parse import urlparse
from collections import defaultdict
from typing import List, Tuple, Optional
ERROR_PREFIX = "Not a redirect error:"defextract_host(url: str) -> Optional[str]:
parsed = urlparse(url)
return parsed.hostname orNonedeffind_error_hosts(log_lines: List[str], top_n: int) -> List[Tuple[str, int]]:
host_counts: dict = defaultdict(int)
for line in log_lines:
line = line.strip()
if not line:
continuetry:
entry = json.loads(line)
except json.JSONDecodeError:
continueif entry.get("level") != "ERROR":
continue
fields = entry.get("fields", {})
message = fields.get("message", "")
if not message.startswith(ERROR_PREFIX):
continue
host = extract_host(fields.get("url", ""))
if host:
host_counts[host] += 1
ranked = sorted(host_counts.items(), key=lambda x: -x[1])
return ranked[:top_n]
Key ideas: Split into a helper (extract_host) and a main function — clean decomposition.
Use defaultdict(int) to avoid key-existence checks. Filter with .get() safely — never assume a key exists in parsed JSON.
startswith is O(k) on prefix length. Final sort is O(k log k) where k = unique hostnames.
Overall complexity: O(n + k log k).
P 02Coding ProblemMedium
Apache Access Log — Top-N IPs with 4xx Errors
Apache logs have lines like: 127.0.0.1 - frank [10/Oct/2000:13:55:36 -0700] "GET /index.html HTTP/1.1" 404 2326
Parse each line, extract the IP (field 0) and status code (field 8).
Count IPs where the status is in the 400–499 range. Return top-N by count.
from collections import defaultdict
from typing import List, Tuple
deftop_ips_4xx(log_lines: List[str], top_n: int) -> List[Tuple[str, int]]:
counts: dict = defaultdict(int)
for line in log_lines:
line = line.strip()
if not line:
continue
parts = line.split()
iflen(parts) < 9:
continue# malformed line
ip = parts[0]
try:
status = int(parts[8])
except ValueError:
continueif400 <= status < 500:
counts[ip] += 1returnsorted(counts.items(), key=lambda x: -x[1])[:top_n]
Key ideas: Apache log fields are space-separated but the date is in brackets, making it tricky to parse.
A simple split() still places the status code at index 8 because the quoted request string is one token.
Always guard with len(parts) < 9 to handle corrupt lines. The range check 400 <= status < 500 is more Pythonic than status // 100 == 4.
P 03Coding ProblemMedium
Slow API Endpoint Finder
Given JSON-lines logs where each entry has endpoint and response_ms fields,
compute the average response time per endpoint and return the top-N slowest as (endpoint, avg_ms) tuples.
Round averages to 1 decimal place.
import json
from collections import defaultdict
from typing import List, Tuple
defslowest_endpoints(log_lines: List[str], top_n: int) -> List[Tuple[str, float]]:
times: dict = defaultdict(list)
for line in log_lines:
line = line.strip()
if not line:
continuetry:
entry = json.loads(line)
except json.JSONDecodeError:
continue
endpoint = entry.get("endpoint")
ms = entry.get("response_ms")
if endpoint and ms is not None:
times[endpoint].append(float(ms))
# Compute average for each endpoint
avgs = [
(ep, round(sum(t) / len(t), 1))
for ep, t in times.items()
]
returnsorted(avgs, key=lambda x: -x[1])[:top_n]
Key ideas: Use defaultdict(list) to accumulate a list of response times per endpoint.
Then compute the mean in a list comprehension: sum(t) / len(t).
This pattern — collect then compute — is cleaner than maintaining a running sum and count separately.
P 04Coding ProblemMedium
Failed Login Detector — Consecutive Failures
Given a CSV string with columns timestamp,user,action where action is LOGIN_SUCCESS or LOGIN_FAIL,
find all users who had 3 or more consecutive failures (a success resets the counter).
Return a sorted list of flagged usernames.
csv.DictReaderio.StringIOdefaultdict(int)stateful iterationcounter reset on successset for flagged users
Solution
import csv
from io import StringIO
from collections import defaultdict
from typing import List
deffind_brute_force_users(csv_text: str, threshold: int = 3) -> List[str]:
reader = csv.DictReader(StringIO(csv_text))
consec_fails: dict = defaultdict(int)
flagged: set = set()
for row in reader:
user = row["user"]
action = row["action"]
if action == "LOGIN_FAIL":
consec_fails[user] += 1if consec_fails[user] >= threshold:
flagged.add(user)
else:
consec_fails[user] = 0# success resets the streakreturnsorted(flagged)
Key ideas:csv.DictReader(StringIO(text)) lets you parse a CSV string without writing to disk — essential for testing.
The reset-on-success pattern (consec_fails[user] = 0) is a classic stateful-iteration trick.
Using a set for flagged users avoids duplicates if a user hits the threshold multiple times.
P 05Coding ProblemHard
Nginx Rate Abuse Detector
JSON-lines log where each entry has ip and timestamp (ISO 8601 string like "2025-01-16T23:09:51Z").
Find IPs that made more than max_per_minute requests within any single minute.
A "minute" is defined by truncating the timestamp to YYYY-MM-DDTHH:MM.
Return sorted list of abusive IPs.
Example
lines with ip "1.1.1.1" making 3 requests in "2025-01-16T23:09":
abusive_ips(lines, max_per_minute=2) → ["1.1.1.1"]
json.loadsstring slicing for time truncationnested defaultdictCounterany() on dict valuessorted()
Solution
import json
from collections import defaultdict, Counter
from typing import List
defabusive_ips(log_lines: List[str], max_per_minute: int) -> List[str]:
# ip -> minute_bucket -> request_count
ip_buckets: dict = defaultdict(Counter)
for line in log_lines:
line = line.strip()
if not line:
continuetry:
entry = json.loads(line)
except json.JSONDecodeError:
continue
ip = entry.get("ip")
ts = entry.get("timestamp", "")
if not ip or not ts:
continue# "2025-01-16T23:09:51Z"[:16] → "2025-01-16T23:09"
minute_bucket = ts[:16]
ip_buckets[ip][minute_bucket] += 1# Flag IPs where any single minute exceeds the limit
flagged = [
ip
for ip, buckets in ip_buckets.items()
ifany(count > max_per_minute for count in buckets.values())
]
returnsorted(flagged)
Key ideas: Slicing ts[:16] truncates an ISO timestamp to minute precision — much simpler than datetime parsing.
defaultdict(Counter) gives you a nested structure: ip_buckets[ip][minute] += 1 just works.
Using any() in the final list comprehension is idiomatic and short-circuits early.
P 06Coding ProblemMedium
Audit Trail Builder
Given a list of event dicts each with user, resource, action, and timestamp,
build a nested structure: user → resource → [actions in time order].
Return a regular dict (not defaultdict).
nested defaultdictsorted() by keydict comprehensionlambda keylist.append
Solution
from collections import defaultdict
from typing import List, Dict
defbuild_audit_trail(events: List[Dict]) -> Dict:
# nested defaultdict: user -> resource -> [actions]
trail: dict = defaultdict(lambda: defaultdict(list))
for event insorted(events, key=lambda e: e["timestamp"]):
trail[event["user"]][event["resource"]].append(event["action"])
# Convert nested defaultdicts to regular dicts before returningreturn {
user: dict(resources)
for user, resources in trail.items()
}
Key ideas:defaultdict(lambda: defaultdict(list)) creates a two-level nested structure where any access auto-initialises.
Sort the events by timestamp before building the trail so actions appear in time order per resource.
Return a plain dict — callers shouldn't depend on defaultdict behaviour.
P 07Coding ProblemMedium
INI Config File Merger
Parse two INI-style config strings (section headers like [database], key-value lines like host=localhost).
Merge them into a flat dict of "section.key" → value. If both files define the same key, the second file wins.
Ignore blank lines and lines starting with #.
from typing import Dict
def_parse_ini(text: str) -> Dict[str, str]:
result: dict = {}
section = "default"for line in text.splitlines():
line = line.strip()
if not line or line.startswith("#"):
continueif line.startswith("[") and line.endswith("]"):
section = line[1:-1] # strip the bracketselif"="in line:
key, _, val = line.partition("=")
result[f"{section}.{key.strip()}"] = val.strip()
return result
defmerge_configs(config1: str, config2: str) -> Dict[str, str]:
merged = _parse_ini(config1)
merged.update(_parse_ini(config2)) # config2 wins on conflictreturn merged
Key ideas: A simple state machine — track the current section as you scan lines.
str.partition('=') is safer than split('=', 1) — it always returns exactly 3 parts, never raises.
Helper function _parse_ini keeps the logic reusable. Merge is just dict.update — second dict wins.
P 08Coding ProblemHard
Multi-Service Log Level Summary
JSON-lines log where each entry has service and level fields.
For each service, count how many lines appear at each log level (DEBUG, INFO, WARN, ERROR).
Return a list of dicts sorted by service name, where each dict is {"service": ..., "counts": {level: count}}.
json.loadsdefaultdict(Counter)sorted() by service namelist of dicts outputdict() conversion
Solution
import json
from collections import defaultdict, Counter
from typing import List, Dict
deflog_level_summary(log_lines: List[str]) -> List[Dict]:
# service name -> Counter of log levels
data: dict = defaultdict(Counter)
for line in log_lines:
line = line.strip()
if not line:
continuetry:
entry = json.loads(line)
except json.JSONDecodeError:
continue
service = entry.get("service", "unknown")
level = entry.get("level", "UNKNOWN")
data[service][level] += 1return [
{"service": svc, "counts": dict(counts)}
for svc, counts insorted(data.items())
]
Key ideas:defaultdict(Counter) is the go-to pattern for "count things within groups".
data[service][level] += 1 — two levels of auto-initialisation in one line.
The final list comprehension converts the nested structure to the required output format.
Convert Counter to dict before returning so callers get a plain dict.
P 09Coding ProblemHard
PII Redactor — Offset-Based API
A PII detection API returns a list of entities as {"start_offset", "end_offset", "type"}.
Given the original text and that list, replace each entity in-place with [TYPE] (e.g. [NAME]).
Also return a count dict of how many times each PII type appeared.
Key constraint: replacements must be applied right-to-left. Why? Because replacing text at position 11 changes the length of the string, shifting all subsequent offsets.
Example
text = "My name is Alice Smith and my email is alice@example.com."
entities = [
{"start_offset": 11, "end_offset": 22, "type": "NAME"},
{"start_offset": 39, "end_offset": 56, "type": "EMAIL_ADDRESS"},
]
redact_pii(text, entities)
→ ("My name is [NAME] and my email is [EMAIL_ADDRESS].",
{"NAME": 1, "EMAIL_ADDRESS": 1})
string slicing with offsetsreversed() on sorted listCounterlist of dictsright-to-left replacement trick
Solution
from collections import Counter
from typing import List, Dict, Tuple
defredact_pii(text: str, entities: List[Dict]) -> Tuple[str, Dict[str, int]]:
if not entities:
return text, {}
# Sort by start_offset ascending so reversed() gives right-to-left order
sorted_entities = sorted(entities, key=lambda e: e["start_offset"])
pii_count = Counter(e["type"] for e in sorted_entities)
# Apply replacements RIGHT-TO-LEFT: each replacement may change string length,# but it only affects positions to its LEFT — which we haven't processed yet.for entity inreversed(sorted_entities):
start = entity["start_offset"]
end = entity["end_offset"]
ptype = entity["type"]
text = text[:start] + f"[{ptype}]" + text[end:]
return text, dict(pii_count)
The critical insight — right-to-left replacement:
If you replace text[11:22] first, the string length changes, and the offset 39 for the email no longer points to the right place.
By processing entities from the end of the string backwards, each replacement only affects characters to its right — which have already been processed.
Left-side offsets remain valid.
Count PII types before replacing, since you're iterating over the original list anyway.
Time: O(n·m) where n = string length, m = number of entities (each slice creates a new string).
P 10Coding ProblemHard
Chunked PII Redactor — Long Documents
The PII API has a limit of 1000 characters per call. Given a document longer than 1000 chars,
split it into chunks of at most 1000 chars (split on whitespace, not mid-word), call the API for each chunk,
adjust the returned offsets by the chunk's start position in the full document, then redact the full text.
Return the redacted text and a total pii_count dict.
Example
def fake_pii_api(chunk: str) -> list:
# Simulates the real API — returns entities for the chunk text
...
# The document is 2500 chars. It gets split into 3 chunks:
# chunk 0: text[0:999] → API returns [{start:11, end:22, type:"NAME"}]
# chunk 1: text[999:1998] → API returns [{start:5, end:15, type:"EMAIL_ADDRESS"}]
# Adjusted: {start:999+5, end:999+15} = {start:1004, end:1014}
# Final redaction uses the full-document offsets
chunk by whitespace (no mid-word split)offset adjustment per chunkCounter.update() to merge countslist accumulationright-to-left replacement
Solution
from collections import Counter
from typing import List, Dict, Tuple, Callable
MAX_CHUNK = 1000defsplit_into_chunks(text: str, max_size: int = MAX_CHUNK) -> List[Tuple[int, str]]:
"""Split text into chunks of at most max_size chars, breaking on whitespace.
Returns list of (start_offset_in_full_text, chunk_text)."""
chunks = []
pos = 0while pos < len(text):
end = pos + max_size
if end >= len(text):
chunks.append((pos, text[pos:]))
break# Don't split mid-word: walk back to the last spacewhile end > pos and text[end] != " ":
end -= 1
chunks.append((pos, text[pos:end]))
pos = end + 1# skip the spacereturn chunks
defredact_pii_long(
text: str,
pii_api: Callable[[str], List[Dict]]
) -> Tuple[str, Dict[str, int]]:
chunks = split_into_chunks(text)
all_entities = []
total_count: Counter = Counter()
for chunk_start, chunk_text in chunks:
entities = pii_api(chunk_text)
for entity in entities:
# Adjust offsets: chunk-relative → full-document-relative
adjusted = {
"start_offset": entity["start_offset"] + chunk_start,
"end_offset": entity["end_offset"] + chunk_start,
"type": entity["type"],
}
all_entities.append(adjusted)
total_count[entity["type"]] += 1# Sort all entities, then apply right-to-left on the FULL text
all_entities.sort(key=lambda e: e["start_offset"])
for entity inreversed(all_entities):
s, e, t = entity["start_offset"], entity["end_offset"], entity["type"]
text = text[:s] + f"[{t}]" + text[e:]
return text, dict(total_count)
Key ideas:
Split carefully — never cut a word in half. Walk back from position 1000 to the nearest space.
Each chunk's API result returns offsets relative to that chunk, so add chunk_start to convert them to full-document offsets.
After collecting and adjusting all entities across all chunks, sort once and apply the standard right-to-left replacement on the full document.
Counter.update() (or just += 1 in the loop) merges counts from all chunks.