Problem · Design

Log Ingestion and Query

MediumDatadogFULLTIMEONSITE INTERVIEW

Implement a simplified logging system that supports log ingestion and conditional queries.

Each operation is one of the following strings:

  • ADD timestamp service level message: append one log entry
  • QUERY startTs endTs serviceOr* levelOr* keywordOr*: count matching logs

A log matches a query if startTs <= timestamp <= endTs, service and level match exactly unless the filter is *, and keyword appears as a substring of the message unless the filter is *.

Process all operations in order and return the outputs produced by the QUERY commands.

Function Description

Complete the function runLogIngestionQueries in the editor below.

runLogIngestionQueries has the following parameter:

  1. String[] operations: the operations to execute in order

Returns

int[]: the counts produced by the QUERY operations.

Examples
01 · Example 1
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"]
return = [3, 2, 1]

The three queries count all logs, then all api logs in the first two timestamps, then only the single error log whose message contains failed.

02 · Example 2
operations = ["ADD 1 api INFO login ok", "ADD 2 api INFO logout ok", "ADD 3 api INFO login failed", "QUERY 1 3 api INFO login", "QUERY 1 3 api INFO ok", "QUERY 1 2 api INFO login", "QUERY 3 3 api INFO login"]
return = [2, 2, 1, 1]

Substring matching on the message field allows both successful and failed login events to match the keyword login.

Constraints
  • 1 <= operations.length <= 2 * 10^5
  • Timestamps are non-negative integers.
  • service and level contain no spaces.
  • message may contain spaces and is matched by substring search.
drafts saved locally
public int[] runLogIngestionQueries(String[] operations) {
    // write your code here
}
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"]
expected[3, 2, 1]
sign in to submit