FastPrepLog Ingestion and Query

Log Ingestion and Query

Datadog logoDatadog● MediumFULLTIMEONSITE INTERVIEW

Problem statement

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

Each operation is one of the following strings, with single spaces between its parts:

  • ADD timestamp service level message: append one log entry. The message is everything after the level and may contain spaces.
  • QUERY startTs endTs service level keyword: count the logs added so far that match. Each of service, level and keyword may be *, which matches every log.

A log matches a query if all of these hold:

  • startTs ≤ timestamp ≤ endTs.
  • The log's service is exactly the query's service, unless the filter is *.
  • The log's level is exactly the query's level, unless the filter is *.
  • The query's keyword appears as a contiguous substring of the log's message (not only as a whole word), unless the filter is *.

Process all operations in order and return an array with the count from each QUERY operation, in the order the queries appear.

Function

runLogIngestionQueries(operations: String[]) → int[]

Complete the function runLogIngestionQueries in the editor below.

runLogIngestionQueries has the following parameter:

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

Examples

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.

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 * 105
  • Each operation is ADD timestamp service level message or QUERY startTs endTs service level keyword, with single spaces between its parts.
  • At least one operation is a QUERY.
  • 0 ≤ timestamp ≤ 109 and 0 ≤ startTs ≤ endTs ≤ 109, written as plain decimal integers.
  • A service name has 1 to 10 characters, each a lowercase English letter or a digit.
  • A level has 1 to 10 uppercase English letters.
  • A message has 1 to 20 characters: lowercase English letters, digits and single spaces, with no space at the start or end.
  • In a query, service and level are * or follow the rules above, and keyword is * or 1 to 10 lowercase English letters or digits.

More Datadog problems

All Datadog interview questionsSee Datadog hiring insights
Saved
class Solution {
    public int[] runLogIngestionQueries(String[] operations) {
        // write your code here
    }

}
Checking account…