Structured Log Stream with Queries
Updated
Problem statement
A service writes structured log entries one after another. Between the entries, someone asks questions about the entries seen so far. You get the whole stream as a list of lines lines. Handle the lines in order and return the answer to every query, in order.
Each line is one of three kinds:
"LOG t key1=value1 key2=value2 ..."adds an entry with timetand one or more fields. A field is a key and a value joined by'=', and the fields may come in any order. This line has no output."COUNT a b"asks how many entries so far have a time fromatob, both ends included."COUNT a b key=value"counts only the entries in that range that have the fieldkeywith exactly this value. The answer is the count as a decimal string."TOP k key a b"looks at the entries so far with a time fromatobthat have the fieldkey. It returns thekvalues ofkeythat appear most often, written as"value:count"pieces joined by commas.
In a TOP answer, more frequent values come first. Values with the same count are compared as text, one character at a time by character code, and the smaller one comes first, so "10" comes before "9". If fewer than k values exist, list all of them. If no entry in the range has the key, the answer is "NONE".
A query only sees entries from earlier lines. An entry without the asked key never matches a filter and is skipped by TOP.
Function
processLogStream(lines: String[]) → String[]Examples
Example 1
lines = ["LOG 3 svc=auth level=error","LOG 5 svc=pay level=info","LOG 5 svc=auth level=info user=u7","COUNT 4 5","COUNT 0 10 svc=auth","LOG 9 svc=auth level=error","COUNT 3 9 level=error","TOP 2 svc 0 9"]return = ["2","2","2","auth:3,pay:1"]COUNT 4 5 counts entries with time from 4 to 5: the two entries at time 5, so "2".
COUNT 0 10 svc=auth runs before the entry at time 9 arrives, so only the entries at times 3 and 5 with svc=auth count: "2".
After the entry at time 9 arrives, COUNT 3 9 level=error finds the entries at times 3 and 9: "2". TOP 2 svc 0 9 sees auth 3 times and pay once: "auth:3,pay:1".
Example 2
lines = ["COUNT 0 100","LOG 10 user=ann","LOG 20 user=bob code=404","TOP 3 code 0 100","TOP 1 user 15 15","COUNT 10 20 code=4"]return = ["0","404:1","NONE","0"]The first query comes before any entry, so the count is "0".
TOP 3 code 0 100: only the entry at time 20 has a code field, so the answer has one value, "404:1". TOP 1 user 15 15 looks only at time 15, where there is no entry, so the answer is "NONE".
COUNT 10 20 code=4 needs code to equal 4 exactly. 404 is a different value, so the count is "0".
Example 3
lines = ["LOG 1 host=b","LOG 2 host=a","LOG 2 host=c","LOG 4 host=a","LOG 6 host=c","TOP 3 host 2 6","TOP 1 host 1 2"]return = ["a:2,c:2","a:1"]TOP 3 host 2 6 looks at times 2 to 6: a appears twice, c twice and nothing else. a and c tie at 2, so the smaller value comes first: "a:2,c:2".
TOP 1 host 1 2 sees b, a and c once each. All three tie, so only the smallest value, a, is kept: "a:1".
Constraints
1 <= lines.length <= 30001 <= lines[i].length <= 60- Every line is one of the three kinds above, with tokens separated by single spaces. Every time,
aandbis an integer from0to10^9written without leading zeros, anda <= bin every query. - Entry times never decrease from one
LOGline to the next. - An entry has
1to4fields with distinct keys. A key has1to8lowercase English letters, and a value has1to8lowercase English letters or digits. - In every
TOPquery,1 <= k <= 5, and at most100lines areTOPqueries.
More OpenAI Phone Screen questions
- Secret Number Search with One-Call-Delayed FeedbackPHONE SCREEN · Seen Nov 2026
- Determine the Winner of Three-Card HandsPHONE SCREEN · Seen Nov 2026
- Grid Infection with Recovery After D DaysPHONE SCREEN · Seen Nov 2026
- Sparse Plant Infection SimulationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Light Source Hits: Sample, Compare, and FitPHONE SCREEN · Seen Nov 2026
- Rectangle Point Coverage CountsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Adaptive Calibration ErrorPHONE SCREEN · Seen Nov 2026
- Elevator Scheduling SimulationPHONE SCREEN · Seen Nov 2026
- Evaluate Noisy AnnotatorsPHONE SCREEN · Seen Oct 2026
- Network Endpoint or Cycle BoundaryOA · PHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Interview Slot BookingTrueFoundry · PHONE SCREEN
- Transaction Event StoreCapital One · ONSITE INTERVIEWCapital One interview questions
- Order Manager with Partial Cancellations and Indexed QueriesStripe · ONSITE INTERVIEWStripe interview questions
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- Top K Pins with Insert and QueryPinterest · PHONE SCREENPinterest interview questions
- Snapshot Map with Sparse Version HistorySnorkel AI · PHONE SCREEN
- Worst-Performing Ads by CTR in a Campaign TreeUber · PHONE SCREENUber interview questions
- Camel CardsRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- Maintain a Consistent-Hashing RingApple · PHONE SCREENApple interview questions
- Time-Based Key-Value StoreOracle · PHONE SCREEN · ONSITE INTERVIEWOracle interview questions