Quick Overview

Process a stream that registers word queries and delivers log lines, returning for each log line the ids of earlier queries whose words all appear in it. Tests set matching over a stream, handling of repeated words and whole-word matches, and a design that stays fast with many queries and long streams.

Match Streaming Log Lines Against Registered Multi-Word Queries

Company: Datadog

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

A log query processor reads a stream of events in order. Each event either registers a query or delivers a log line. A query is a set of words, and it matches a log line when every one of its words appears in that log line. For each log line, report which of the queries registered so far match it. Queries receive ids `1`, `2`, `3`, and so on in the order they are registered. Return one list per log line, in stream order. Each list holds the ids of the matching queries registered before that log line, in ascending order. Query events produce no output. ### Function Signature ```python def process_log_stream(events: list[str]) -> list[list[int]]: ``` ### Rules - An event that starts with `"Q: "` registers a query made of the words after the prefix. An event that starts with `"L: "` is a log line made of the words after the prefix. - Words are separated by single spaces. A query word matches only an identical word in the log line; a query word that is only part of a longer log word does not match. - If a query repeats a word, that word needs to appear only once in the log line. - Word order, and how many times a word appears in the log line, do not matter. - A query matches only log lines that arrive after it is registered. - A log line that matches no query produces an empty list. ### Constraints - `1 <= len(events) <= 2 * 10^4` - Every event is `"Q: "` or `"L: "` followed by 1 to 50 words. - Every word has 1 to 20 characters, each a lowercase English letter or a digit. - The total number of words across all events is at most `5 * 10^5`. - The total number of ids across all returned lists is at most `10^6`. ### Examples #### Example 1 ```text Input: events = ["Q: error disk", "Q: timeout", "L: disk error on host7", "L: request timeout after retry", "Q: error", "L: error disk timeout"] Output: [[1], [2], [1, 2, 3]] ``` Query 1 needs both `error` and `disk`, and query 2 needs `timeout`. The first log line contains both words of query 1 and no `timeout`, and the second log line matches only query 2. Query 3 is registered before the last log line, which contains every word of all three queries. #### Example 2 ```text Input: events = ["L: boot ok", "Q: boot boot", "Q: boot fail", "Q: ok", "L: boot ok", "L: booting"] Output: [[], [1, 3], []] ``` The first log line arrives before any query is registered. The second matches query 1 (its repeated word `boot` appears once) and query 3, but not query 2, which also needs `fail`. The last log line contains `booting`, which is not the word `boot`.

Overview: Process a stream that registers word queries and delivers log lines, returning for each log line the ids of earlier queries whose words all appear in it. Tests set matching over a stream, handling of repeated words and whole-word matches, and a design that stays fast with many queries and long streams.

Read the full Datadog Software Engineer interview experience this question came from

A log query processor reads a stream of events in order. Each event either registers a query or delivers a log line. A query is a set of words, and it matches a log line when every one of its words appears in that log line. For each log line, report which of the queries registered so far match it. Queries receive ids `1`, `2`, `3`, and so on in the order they are registered. Return one list per log line, in stream order. Each list holds the ids of the matching queries registered before that log line, in ascending order. Query events produce no output, so a stream without log lines returns an empty list. Implement `process_log_stream(events)`. ### Rules - An event that starts with `"Q: "` registers a query made of the words after the prefix. An event that starts with `"L: "` is a log line made of the words after the prefix. - Words are separated by single spaces. A query word matches only an identical word in the log line; a query word that is only part of a longer log word does not match. - If a query repeats a word, that word needs to appear only once in the log line. - Word order, and how many times a word appears in the log line, do not matter. - A query matches only log lines that arrive after it is registered. - A log line that matches no query produces an empty list. - Every id is at most `len(events)`, so every returned value fits in a signed 32-bit integer. ### Example 1 ```text Input: events = ["Q: error disk", "Q: timeout", "L: disk error on host7", "L: request timeout after retry", "Q: error", "L: error disk timeout"] Output: [[1], [2], [1, 2, 3]] ``` Query 1 needs both `error` and `disk`, and query 2 needs `timeout`. The first log line contains both words of query 1 and no `timeout`, and the second log line matches only query 2. Query 3 is registered before the last log line, which contains every word of all three queries. ### Example 2 ```text Input: events = ["L: boot ok", "Q: boot boot", "Q: boot fail", "Q: ok", "L: boot ok", "L: booting"] Output: [[], [1, 3], []] ``` The first log line arrives before any query is registered. The second matches query 1 (its repeated word `boot` appears once) and query 3. Query 2 also needs `fail`, so it does not match. The last log line contains `booting`, which differs from the word `boot`. ### Constraints - `1 <= len(events) <= 2 * 10^4` - Every event is `"Q: "` or `"L: "` followed by 1 to 50 words separated by single spaces. - Every word has 1 to 20 characters, each a lowercase English letter or a digit. - The total number of words across all events is at most `5 * 10^5`. - The total number of ids across all returned lists is at most `10^6`.

Constraints

  • 1 <= len(events) <= 2 * 10^4
  • Every event is "Q: " or "L: " followed by 1 to 50 words separated by single spaces.
  • Every word has 1 to 20 characters, each a lowercase English letter or a digit.
  • The total number of words across all events is at most 5 * 10^5.
  • The total number of ids across all returned lists is at most 10^6.
  • Every id is at most len(events), so every returned value fits in a signed 32-bit integer.

Examples

Input: (['Q: error disk', 'Q: timeout', 'L: disk error on host7', 'L: request timeout after retry', 'Q: error', 'L: error disk timeout'],)

Expected Output: [[1], [2], [1, 2, 3]]

Explanation: Example 1 from the statement: query 3 registers before the last line, which matches all three queries.

Input: (['L: boot ok', 'Q: boot boot', 'Q: boot fail', 'Q: ok', 'L: boot ok', 'L: booting'],)

Expected Output: [[], [1, 3], []]

Explanation: Example 2 from the statement: a line before any query, a repeated query word, and booting differing from boot.

Hints

  1. Repeated words inside a query or a log line never change the answer, and only whole words count as a match.
  2. Each output list must be in ascending id order, whichever words of the log line you examine first.
  3. At the upper bounds, comparing every log line word by word against every earlier query can take on the order of 10^8 steps; think about which earlier queries a given log line could possibly satisfy.

Match Word Queries Against a Log Stream

mediumDatadogSoftware Engineer

A log query processor reads a stream of events in order. Each event either registers a query or delivers a log line. A query is a set of words, and it matches a log line when every one of its words appears in that log line. For each log line, report which of the queries registered so far match it.

Queries receive ids 1, 2, 3, and so on in the order they are registered. Return one list per log line, in stream order. Each list holds the ids of the matching queries registered before that log line, in ascending order. Query events produce no output, so a stream without log lines returns an empty list.

Implement process_log_stream(events).

Rules

  • An event that starts with "Q: " registers a query made of the words after the prefix. An event that starts with "L: " is a log line made of the words after the prefix.
  • Words are separated by single spaces. A query word matches only an identical word in the log line; a query word that is only part of a longer log word does not match.
  • If a query repeats a word, that word needs to appear only once in the log line.
  • Word order, and how many times a word appears in the log line, do not matter.
  • A query matches only log lines that arrive after it is registered.
  • A log line that matches no query produces an empty list.
  • Every id is at most len(events), so every returned value fits in a signed 32-bit integer.

Example 1

Input:  events = ["Q: error disk", "Q: timeout", "L: disk error on host7", "L: request timeout after retry", "Q: error", "L: error disk timeout"]
Output: [[1], [2], [1, 2, 3]]

Query 1 needs both error and disk, and query 2 needs timeout. The first log line contains both words of query 1 and no timeout, and the second log line matches only query 2. Query 3 is registered before the last log line, which contains every word of all three queries.

Example 2

Input:  events = ["L: boot ok", "Q: boot boot", "Q: boot fail", "Q: ok", "L: boot ok", "L: booting"]
Output: [[], [1, 3], []]

The first log line arrives before any query is registered. The second matches query 1 (its repeated word boot appears once) and query 3. Query 2 also needs fail, so it does not match. The last log line contains booting, which differs from the word boot.

Constraints

  • 1 <= len(events) <= 2 * 10^4
  • Every event is "Q: " or "L: " followed by 1 to 50 words separated by single spaces.
  • Every word has 1 to 20 characters, each a lowercase English letter or a digit.
  • The total number of words across all events is at most 5 * 10^5.
  • The total number of ids across all returned lists is at most 10^6.

Examples

Example 1
Input
(['Q: error disk', 'Q: timeout', 'L: disk error on host7', 'L: request timeout after retry', 'Q: error', 'L: error disk timeout'],)
Output
[[1], [2], [1, 2, 3]]
Notes
Example 1 from the statement: query 3 registers before the last line, which matches all three queries.
Example 2
Input
(['L: boot ok', 'Q: boot boot', 'Q: boot fail', 'Q: ok', 'L: boot ok', 'L: booting'],)
Output
[[], [1, 3], []]
Notes
Example 2 from the statement: a line before any query, a repeated query word, and booting differing from boot.

Constraints

  • •1 <= len(events) <= 2 * 10^4
  • •Every event is "Q: " or "L: " followed by 1 to 50 words separated by single spaces.
  • •Every word has 1 to 20 characters, each a lowercase English letter or a digit.
  • •The total number of words across all events is at most 5 * 10^5.
  • •The total number of ids across all returned lists is at most 10^6.
  • •Every id is at most len(events), so every returned value fits in a signed 32-bit integer.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def process_log_stream(events: list[str]) -> list[list[int]]:
Sample call
process_log_stream(['Q: error disk', 'Q: timeout', 'L: disk error on host7', 'L: request timeout after retry', 'Q: error', 'L: error disk timeout'])
Language
Case 1
Input
(['Q: error disk', 'Q: timeout', 'L: disk error on host7', 'L: request timeout after retry', 'Q: error', 'L: error disk timeout'],)
Expected
[[1], [2], [1, 2, 3]]
Case 2
Input
(['L: boot ok', 'Q: boot boot', 'Q: boot fail', 'Q: ok', 'L: boot ok', 'L: booting'],)
Expected
[[], [1, 3], []]

Browse More Questions

Show the approach

Approach

Read the stream twice. The first pass counts, for every word, how many log lines contain it. The second pass walks the events in order and keeps that count current as remaining[w], the number of log lines from the current position onward that contain w: each processed log line subtracts one for each of its distinct words.

When a query arrives it gets the next id, its duplicate words are dropped, and it is filed under one anchor word, the query word with the smallest remaining count. If that count is zero, one of its words appears in no later log line, so the query can never match and is not filed. When a log line arrives, its distinct words form a set. For each word in the set, every query filed under that word is tested, and it matches when all of its words are in the set. The matched ids are sorted and appended as that line's list.

Correctness: every filed query has exactly one anchor, and the anchor is one of its own words. If a query matches a line, its anchor is in the line's set, so the query is tested; because the set holds each word once, it is tested once and no id repeats. A query is filed only when it is registered, so log lines before it never see it. Membership is tested on whole words, so booting never satisfies boot, and using sets on both sides makes repeated words irrelevant. Sorting puts the ids in ascending order. Skipping a query whose anchor count is zero loses nothing, because a match would need that word in some later line.

Edge cases: a stream of only queries returns [], and a log line before any query, or one that matches nothing, contributes [].

Time complexity:
O(W + T + M log M), where W is the total number of words, T is the number of word membership tests made on filed queries, and M is the total number of returned ids
Space complexity:
O(W + M)