Datadog questions

Log Ingestion and Query

DatadogOnsiteMedium

A logging service stores log lines and answers count queries. Implement a class LogStore that starts empty and supports adding logs and querying counts of stored logs.

Part 1: Add logs and count matches

You are given a logging service that stores log lines and answers count queries. Implement a class LogStore with two methods.

add(timestamp, service, level, message) stores one log. service and level are single words, and message is text that may contain spaces.

query(start, end, service, level, keyword) returns how many stored logs satisfy all of these conditions:

  • start <= timestamp <= end
  • the log's service equals service
  • the log's level equals level
  • keyword is a substring of the log's message

Matching is case-sensitive. The value * for service, level or keyword means that condition is not applied.

Logs can be added with timestamps in any order, and query may be called between adds. A log with the same fields as an earlier one counts separately.

Start from this skeleton:

python class LogStore: def __init__(self) -> None: pass

def add(self, timestamp: int, service: str, level: str, message: str) -> None: raise NotImplementedError

def query(self, start: int, end: int, service: str, level: str, keyword: str) -> int: raise NotImplementedError

  1. Example 1
    init
    []
    operations
    [["add",[1,"api","INFO","hello world"]],["add",[2,"api","ERROR","something failed"]],["add",[3,"web","INFO","user login"]],["query",[1,3,"*","*","*"]],["query",[1,2,"api","*","*"]],["query",[2,3,"api","ERROR","failed"]]]
    Output
    [null,null,null,3,2,1]

    Why: The three logs sit in times 1 to 3, so the unfiltered query counts 3. The second query keeps the two api logs. The third needs service api, level ERROR and failed in the message, which only the log at time 2 satisfies.

Constraints

  • 1 <= total number of add and query calls <= 2 * 10^5
  • 0 <= timestamp <= 10^9
  • 0 <= start <= end <= 10^9
  • service and level of an added log are non-empty, contain no spaces, and are not *
  • 1 <= len(message) <= 200
  • keyword is * or 1 <= len(keyword) <= 200
Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question
Log Ingestion and Query: Datadog interview question | LoopCoach