Design a Log Parser With Level, Time-Range and Event-Count Queries

Quick Overview

Design a log parser class that stores entries of the form timestamp:level:message and answers queries by severity level, by inclusive time range, and by event count per level. It tests careful string parsing, picking an index for each query type, and binary search over timestamps that arrive in sorted order.

Design a Log Parser With Level, Time-Range and Event-Count Queries

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design a utility that parses log entries, extracts their fields, and tracks event counts in near real time. Each log entry is a string in the format `timestamp:level:message`, where `timestamp` is an integer, `level` is one of `INFO`, `WARN` or `ERROR`, and `message` is an arbitrary string that may contain colons. Log entries are always added in non-decreasing timestamp order. The parser supports adding log entries, querying logs by severity level, querying logs within a time range, and retrieving the event count for a given level. Implement a class `LogParser` with these methods: - `addLog(log: str)` parses a log entry in the format `timestamp:level:message` and stores it. - `getLogsByLevel(level: str) -> List[str]` returns all log entries with the given level, as formatted strings, in the order they were added. - `getLogsByTimeRange(start: int, end: int) -> List[str]` returns all log entries whose timestamp is in the inclusive range `[start, end]`, as formatted strings, in the order they were added. - `getEventCount(level: str) -> int` returns the total number of log entries recorded for the given level. A formatted string is the entry written back as `timestamp:level:message`. Example 1, queries before and after the first entry: ```text parser = LogParser() parser.getLogsByLevel("INFO") -> [] parser.getEventCount("ERROR") -> 0 parser.addLog("50:INFO:Start") parser.getLogsByLevel("INFO") -> ["50:INFO:Start"] parser.getLogsByTimeRange(1, 100) -> ["50:INFO:Start"] ``` Both early queries run on an empty parser, so they return an empty list and a count of 0. Example 2, a message that contains a colon and two entries with the same timestamp: ```text parser = LogParser() parser.addLog("10:INFO:boot") parser.addLog("20:ERROR:disk:full") parser.addLog("20:WARN:retry") parser.addLog("35:ERROR:timeout:db") parser.getLogsByLevel("ERROR") -> ["20:ERROR:disk:full", "35:ERROR:timeout:db"] parser.getLogsByTimeRange(20, 35) -> ["20:ERROR:disk:full", "20:WARN:retry", "35:ERROR:timeout:db"] parser.getEventCount("ERROR") -> 2 parser.getEventCount("WARN") -> 1 ``` The messages `disk:full` and `timeout:db` keep their colons. The range query returns both entries at timestamp 20 and the entry at 35 because both bounds are inclusive. ```hint Splitting the line The message can contain colons. Decide how many splits you need to get the three fields. ``` ```hint Range queries Entries arrive already sorted by timestamp. Think about what that ordering lets you do when you look for the first and last entry inside a range. ``` ### Constraints & Assumptions - `1 <= timestamp <= 10^9` - `level` is one of `"INFO"`, `"WARN"`, `"ERROR"` - `1 <= message.length <= 200` - Log entries are added in non-decreasing timestamp order, so several entries can share a timestamp - At most `10^4` calls to all methods combined - For `getLogsByTimeRange`, `start <= end` ### Clarifying Questions to Ask - Should the returned strings be the original lines exactly, or lines rebuilt from the parsed fields (they differ only if a timestamp could have leading zeros)? - Can a line be malformed or carry an unknown level, and if so should it be rejected, skipped or counted? - Will callers mutate the lists they get back, and should that affect the stored logs? - Is the parser used from a single thread, or do writes and queries run concurrently? ### What a Strong Answer Covers - Parsing that keeps colons inside the message intact and stores the timestamp as a number. - Which structure answers each query, and how all of them stay consistent on every insert. - How the sorted arrival order is used for time-range queries, with inclusive bounds and repeated timestamps handled correctly. - Time and space complexity for each method. - Behavior on empty input: queries before any log, levels with no entries, and ranges that match nothing. ### Follow-up Questions - How would the design change if log entries could arrive out of timestamp order? - How would you answer a query that filters by level and time range together? - Memory is limited. How would you keep only the last N minutes of logs while keeping the counts correct? - What breaks if several threads call `addLog` and the query methods at the same time, and how would you fix it?

Overview: Design a log parser class that stores entries of the form timestamp:level:message and answers queries by severity level, by inclusive time range, and by event count per level. It tests careful string parsing, picking an index for each query type, and binary search over timestamps that arrive in sorted order.

Nov 25, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a utility that parses log entries, extracts their fields, and tracks event counts in near real time.

Each log entry is a string in the format timestamp:level:message, where timestamp is an integer, level is one of INFO, WARN or ERROR, and message is an arbitrary string that may contain colons. Log entries are always added in non-decreasing timestamp order.

The parser supports adding log entries, querying logs by severity level, querying logs within a time range, and retrieving the event count for a given level. Implement a class LogParser with these methods:

  • addLog(log: str) parses a log entry in the format timestamp:level:message and stores it.
  • getLogsByLevel(level: str) -> List[str] returns all log entries with the given level, as formatted strings, in the order they were added.
  • getLogsByTimeRange(start: int, end: int) -> List[str] returns all log entries whose timestamp is in the inclusive range [start, end] , as formatted strings, in the order they were added.
  • getEventCount(level: str) -> int returns the total number of log entries recorded for the given level.

A formatted string is the entry written back as timestamp:level:message.

Example 1, queries before and after the first entry:

parser = LogParser()
parser.getLogsByLevel("INFO")       -> []
parser.getEventCount("ERROR")       -> 0
parser.addLog("50:INFO:Start")
parser.getLogsByLevel("INFO")       -> ["50:INFO:Start"]
parser.getLogsByTimeRange(1, 100)   -> ["50:INFO:Start"]

Both early queries run on an empty parser, so they return an empty list and a count of 0.

Example 2, a message that contains a colon and two entries with the same timestamp:

parser = LogParser()
parser.addLog("10:INFO:boot")
parser.addLog("20:ERROR:disk:full")
parser.addLog("20:WARN:retry")
parser.addLog("35:ERROR:timeout:db")
parser.getLogsByLevel("ERROR")      -> ["20:ERROR:disk:full", "35:ERROR:timeout:db"]
parser.getLogsByTimeRange(20, 35)   -> ["20:ERROR:disk:full", "20:WARN:retry", "35:ERROR:timeout:db"]
parser.getEventCount("ERROR")       -> 2
parser.getEventCount("WARN")        -> 1

The messages disk:full and timeout:db keep their colons. The range query returns both entries at timestamp 20 and the entry at 35 because both bounds are inclusive.

Constraints & Assumptions

  • 1 <= timestamp <= 10^9
  • level is one of "INFO" , "WARN" , "ERROR"
  • 1 <= message.length <= 200
  • Log entries are added in non-decreasing timestamp order, so several entries can share a timestamp
  • At most 10^4 calls to all methods combined
  • For getLogsByTimeRange , start <= end

Clarifying Questions to Ask Guidance

  • Should the returned strings be the original lines exactly, or lines rebuilt from the parsed fields (they differ only if a timestamp could have leading zeros)?
  • Can a line be malformed or carry an unknown level, and if so should it be rejected, skipped or counted?
  • Will callers mutate the lists they get back, and should that affect the stored logs?
  • Is the parser used from a single thread, or do writes and queries run concurrently?

What a Strong Answer Covers Guidance

  • Parsing that keeps colons inside the message intact and stores the timestamp as a number.
  • Which structure answers each query, and how all of them stay consistent on every insert.
  • How the sorted arrival order is used for time-range queries, with inclusive bounds and repeated timestamps handled correctly.
  • Time and space complexity for each method.
  • Behavior on empty input: queries before any log, levels with no entries, and ranges that match nothing.

Follow-up Questions Guidance

  • How would the design change if log entries could arrive out of timestamp order?
  • How would you answer a query that filters by level and time range together?
  • Memory is limited. How would you keep only the last N minutes of logs while keeping the counts correct?
  • What breaks if several threads call addLog and the query methods at the same time, and how would you fix it?