Kikoff logoKikoff
CodingFree preview

Log Parser with Query Operators

Part 1: parse List[str] logs into {timestamp, level, service, message, user_id}, dropping malformed lines. Part 2: search with leading type:value operators (level, service, userid, inclusive start, exclusive end) plus a trailing message keyword, returning the matches and their count.

SWE
parsing
string-processing
medium
Frequency
Single report
Last asked
2026-08-07
Stage
phone-screen

Problem

Build a tool that parses and queries a set of server logs. Input is a list of log strings (List[str]) coming from different services.

Part 1: Parse and Filter

Implement a function that reads the logs and parses each into a structured object:

{
  "timestamp": datetime or str,
  "level": "INFO" | "WARN" | "ERROR" | "DEBUG",
  "service": str,
  "message": str,
  "user_id": str or None   # filled if a user_id can be extracted from message, else None
}
  • Raw line format: YYYY-MM-DDTHH:MM:SS LEVEL SERVICE MESSAGE. LEVEL is one of INFO / WARN / ERROR / DEBUG, SERVICE contains no whitespace, and MESSAGE is everything after it.
  • user_id is the first id in MESSAGE written as user_id=<id> or user_id:<id>, where <id> consists of letters, digits, underscore or hyphen; otherwise None.
  • Input lines may be malformed or not follow the format; drop any line with a missing field, an invalid timestamp, or an invalid level.
  • Return List[ParsedLog] containing every successfully parsed log.

Part 2: Search Query with Operators

Implement a search function over the parsed logs (up to 10^5 lines). The query string may begin with any number of type:value operators; all operators sit at the start of the query.

  • level:info / level:warn / level:error / level:debug — filter by level.
  • service:<name> (e.g. service:auth, service:payment, service:user) — filter by service.
  • userid:<id> (e.g. userid:12345) — only logs belonging to that user.
  • start:<date> (e.g. start:2024-01-01) — timestamp >= date 00:00:00 (inclusive).
  • end:<date> (e.g. end:2024-01-31) — timestamp < date 00:00:00 (exclusive).
  • Operators are combined with AND. level values are case-insensitive; service and userid are exact matches.
  • Any text remaining after the operators is a case-insensitive substring search against message.

Return all matching logs in input order and the match count. The exact shape is your choice, e.g. {"count": 12, "logs": [...]} or (List[ParsedLog], count).

Examples

  • level:error service:auth timeout — level = ERROR, service = auth, and message contains "timeout".
  • level:error service:auth — all ERROR logs from the auth service.
  • level:info payment processed — level = INFO and message contains payment processed.
  • userid:12345 — all logs produced by that user.
  • start:2024-01-01 end:2024-02-01 — 2024-01-01 <= timestamp < 2024-02-01.

Pitfalls

  • The raw log-line layout and the way a user_id appears inside message are sometimes not spelled out in the prompt — confirm both with the interviewer before writing the parser.
  • A line with an unknown level, an invalid timestamp or fewer than four fields must be dropped, not parsed with defaults.
  • Only the first user_id in a message counts; a second one later in the message is ignored.
  • Operator values are lowercase (level:error) while parsed levels are uppercase (ERROR); normalize case before comparing.
  • start is inclusive and end is exclusive. Off-by-one on the date bounds is the easiest bug to ship.
  • Comparing dates as strings only works if every timestamp is in the same sortable format; if you keep timestamp as a string, say so explicitly.
  • Parsing stops consuming operators at the first token that is not type:value; everything after it, including multi-word text like payment processed, is the keyword.
  • The function must return the count as well as the list.

Preparation

  • In 25 minutes, implement Part 1 with a single regex or split per line, and test it on a malformed line, a line with an unknown level, and a line with no user id.
  • Implement Part 2 as a parse-query step (operators dict + remaining keyword) followed by one filter pass; test each of the five example queries above.
  • Practice explaining how you would add a new operator type without touching the filter loop (e.g. a table of operator → predicate).
Was this article helpful?

Comments

Sign in to join the discussion
Loading...