Match Streaming Log Lines Against Registered Multi-Word Queries
Company: Datadog
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
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
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
- Repeated words inside a query or a log line never change the answer, and only whole words count as a match.
- Each output list must be in ascending id order, whichever words of the log line you examine first.
- 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.