As reported: Datadog Senior Frontend onsite (Apr 2025), coding round 2: parse a stream of lines where
Q:lines are queries andL:lines are logs, and show which queries match each log.
You receive a stream of lines, each either a query Q: <words> or a log L: <words>, in order:
- For each query, assign the next id (starting at
1) and outputACK: <query text>; ID=<id>. - For each log, find every query registered so far whose words all appear in the log, and output
M: <log text>; Q=<ids>with the ids ascending and comma-separated. If no query matches, output nothing for that log.
A query matches a log when every word of the query appears as a whole word in the log, ignoring case. Words are separated by spaces; a repeated word in a query counts once.
Example
Input Output
Q: database ACK: database; ID=1
Q: Stacktrace ACK: Stacktrace; ID=2
Q: loading failed ACK: loading failed; ID=3
L: Database service started M: Database service started; Q=1
Q: snapshot loading ACK: snapshot loading; ID=4
Q: fail ACK: fail; ID=5
L: Started processing events (no output)
L: Loading main DB snapshot M: Loading main DB snapshot; Q=4
L: Loading snapshot failed no stacktrace available M: Loading snapshot failed no stacktrace available; Q=2,3,4
fail does not match failed: matching is on whole words.
Constraints: at most 2·10^4 lines; at most 12 words per line; words are letters and digits.
Code runner format: the input is the stream itself, one line per query or log. Implement processStream(lines) returning the output lines in order; the harness prints them.