FastPrepFastPrep
Problem Brief

Log Ingestion and Query

FULLTIMEONSITE 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.

1Example 1

Input
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
[3, 2, 1]
Explanation

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.

2Example 2

Input
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"]
Output
[2, 2, 1, 1]
Explanation

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

Constraints

Limits and guarantees your solution can rely on.

  • 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.
public int[] runLogIngestionQueries(String[] operations) {
    // write your code here
}
Input

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

[3, 2, 1]

Sign in to submit your solution.