Implement a log-query stream processor with dynamic keyword queries
Quick Overview
Implement a streaming log processor where keyword queries can be registered and unregistered at any time and each log is matched by case-insensitive substring. It tests picking data structures that answer query-to-logs and log-to-queries lookups quickly while keeping past matches after a query is removed.
Implement a log-query stream processor with dynamic keyword queries
Company: Datadog
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a streaming log processor that ingests a mixed stream of log messages and allows users to dynamically register and unregister keyword-based queries at any time, without restarting the processor.
When a log message is ingested, it is checked against all currently active queries. A log matches a query if the query's keyword appears as a case-insensitive substring in the log message. The processor should track which logs matched which queries. Logs are indexed from 0 in ingestion order.
The processor must support retrieving all logs that matched a specific query (in ingestion order), and all query IDs that matched a specific log (sorted lexicographically). Unregistering a query stops it from matching future logs, but **past matches are preserved**.
Implement the following methods:
- `register(query_id: str, keyword: str) -> bool`: Register a new query with the given ID and keyword for substring matching. Returns `True` if registered successfully, `False` if the query ID already exists.
- `unregister(query_id: str) -> bool`: Unregister an existing query by ID. Future logs will no longer be matched against it, but past matches are preserved. Returns `True` if removed, `False` if not found.
- `ingest(message: str) -> int`: Ingest a log message and check it against all currently active queries using case-insensitive substring matching. Returns the number of queries that matched.
- `get_logs_for_query(query_id: str) -> List[str]`: Return all log messages that matched the given query ID, in ingestion order. Returns an empty list if the query ID has no matches.
- `get_queries_for_log(log_index: int) -> List[str]`: Return all query IDs that matched the log at the given index, sorted lexicographically. Returns an empty list if the index is invalid.
- `get_log_count() -> int`: Return the total number of logs ingested so far.
For example, this sequence of calls returns the values on the right:
```text
register("q1", "error") -> True
register("q2", "DB") -> True
register("q1", "timeout") -> False
ingest("ERROR: db connection lost") -> 2
ingest("user login ok") -> 0
unregister("q2") -> True
ingest("Error writing to DB") -> 1
get_logs_for_query("q1") -> ["ERROR: db connection lost", "Error writing to DB"]
get_logs_for_query("q2") -> ["ERROR: db connection lost"]
get_queries_for_log(0) -> ["q1", "q2"]
get_queries_for_log(2) -> ["q1"]
get_queries_for_log(5) -> []
get_log_count() -> 3
```
Log 0 contains both "error" and "db" once case is ignored, so it matches both queries. Log 2 also contains "db", but `q2` was unregistered before it arrived, so only `q1` matches it, and `q2` keeps its earlier match on log 0. Index 5 does not exist, so that lookup returns an empty list.
A second sequence shows that a keyword can match inside a longer word:
```text
register("t", "TIME") -> True
ingest("Request timeout after 30s") -> 1
ingest("runtime panic") -> 1
ingest("all good") -> 0
get_logs_for_query("t") -> ["Request timeout after 30s", "runtime panic"]
get_queries_for_log(2) -> []
get_logs_for_query("missing") -> []
```
The keyword "time" appears inside both "timeout" and "runtime". Log 2 exists but matched nothing, and a query ID that was never registered has no matches.
```hint Two lookup directions
The two read methods ask opposite questions: query to logs, and log to queries. Decide what `ingest` should record so that neither read has to look at old logs again.
```
```hint When matching has to happen
Think about which query IDs should come back for an old log after one of its queries was removed, or after a new query was added. That tells you whether a match can be computed at read time.
```
### Constraints & Assumptions
- `1 <= message.length <= 1000`
- `1 <= keyword.length <= 100`
- Query IDs are unique strings.
- All matching is case-insensitive substring matching.
- At most 10,000 operations in total.
### Clarifying Questions to Ask
- Can a query ID be registered again after it has been unregistered, and if so, should `get_logs_for_query` return the matches from both registrations?
- Does case-insensitive mean ASCII letters only, or full Unicode case folding?
- Will `ingest`, `register` and `unregister` be called from more than one thread?
- Does the processor keep every log forever, or is there a retention limit?
### What a Strong Answer Covers
- Separate state for the active queries and for the match history, and how `unregister` changes one without touching the other.
- What `ingest` records, and how that keeps both lookups from rescanning old logs or re-running matches.
- The ordering guarantees: ingestion order for a query's logs and lexicographic order for a log's query IDs.
- Edge cases: duplicate registration, unknown IDs, negative or out-of-range indices, logs that match nothing, and reuse of a removed ID.
- Time and space cost of each method, with the cost of `ingest` stated in terms of active queries and message length.
### Follow-up Questions
- With tens of thousands of active keywords, checking each one against every log is too slow. How would you find all matching keywords in one pass over the message, and how would you handle queries that change while logs keep arriving?
- The stream never ends. How would you bound memory, and what should the lookups return for logs that have been evicted?
- How would you make the processor safe when `ingest`, `register` and `unregister` run on several threads?
- How would you support whole-word matching, or queries made of several keywords?
Overview: Implement a streaming log processor where keyword queries can be registered and unregistered at any time and each log is matched by case-insensitive substring. It tests picking data structures that answer query-to-logs and log-to-queries lookups quickly while keeping past matches after a query is removed.
Design a streaming log processor that ingests a mixed stream of log messages and allows users to dynamically register and unregister keyword-based queries at any time, without restarting the processor.
When a log message is ingested, it is checked against all currently active queries. A log matches a query if the query's keyword appears as a case-insensitive substring in the log message. The processor should track which logs matched which queries. Logs are indexed from 0 in ingestion order.
The processor must support retrieving all logs that matched a specific query (in ingestion order), and all query IDs that matched a specific log (sorted lexicographically). Unregistering a query stops it from matching future logs, but past matches are preserved.
Implement the following methods:
register(query_id: str, keyword: str) -> bool
: Register a new query with the given ID and keyword for substring matching. Returns
True
if registered successfully,
False
if the query ID already exists.
unregister(query_id: str) -> bool
: Unregister an existing query by ID. Future logs will no longer be matched against it, but past matches are preserved. Returns
True
if removed,
False
if not found.
ingest(message: str) -> int
: Ingest a log message and check it against all currently active queries using case-insensitive substring matching. Returns the number of queries that matched.
get_logs_for_query(query_id: str) -> List[str]
: Return all log messages that matched the given query ID, in ingestion order. Returns an empty list if the query ID has no matches.
get_queries_for_log(log_index: int) -> List[str]
: Return all query IDs that matched the log at the given index, sorted lexicographically. Returns an empty list if the index is invalid.
get_log_count() -> int
: Return the total number of logs ingested so far.
For example, this sequence of calls returns the values on the right:
Log 0 contains both "error" and "db" once case is ignored, so it matches both queries. Log 2 also contains "db", but q2 was unregistered before it arrived, so only q1 matches it, and q2 keeps its earlier match on log 0. Index 5 does not exist, so that lookup returns an empty list.
A second sequence shows that a keyword can match inside a longer word:
The keyword "time" appears inside both "timeout" and "runtime". Log 2 exists but matched nothing, and a query ID that was never registered has no matches.
Constraints & Assumptions
1 <= message.length <= 1000
1 <= keyword.length <= 100
Query IDs are unique strings.
All matching is case-insensitive substring matching.
At most 10,000 operations in total.
Clarifying Questions to Ask Guidance
Can a query ID be registered again after it has been unregistered, and if so, should
get_logs_for_query
return the matches from both registrations?
Does case-insensitive mean ASCII letters only, or full Unicode case folding?
Will
ingest
,
register
and
unregister
be called from more than one thread?
Does the processor keep every log forever, or is there a retention limit?
What a Strong Answer Covers Guidance
Separate state for the active queries and for the match history, and how
unregister
changes one without touching the other.
What
ingest
records, and how that keeps both lookups from rescanning old logs or re-running matches.
The ordering guarantees: ingestion order for a query's logs and lexicographic order for a log's query IDs.
Edge cases: duplicate registration, unknown IDs, negative or out-of-range indices, logs that match nothing, and reuse of a removed ID.
Time and space cost of each method, with the cost of
ingest
stated in terms of active queries and message length.
Follow-up Questions Guidance
With tens of thousands of active keywords, checking each one against every log is too slow. How would you find all matching keywords in one pass over the message, and how would you handle queries that change while logs keep arriving?
The stream never ends. How would you bound memory, and what should the lookups return for logs that have been evicted?
How would you make the processor safe when
ingest
,
register
and
unregister
run on several threads?
How would you support whole-word matching, or queries made of several keywords?