Recent Event Stream Queries
Updated
Problem statement
Process a finite ordered batch of string operations while retaining only the most recently recorded m events. Each recorded event has a timestamp and key.
Operations have these forms:
record timestamp key: append an event. Record timestamps are nondecreasing. If the retained window now contains more thanmevents, discard its oldest event.count timestamp: return the number of distinct keys among retained events whose event timestamp is strictly less thantimestamp.top: return the most frequent key in the complete retained window. Break frequency ties by lexicographically smaller key. Return the empty string when the window is empty.
A record operation produces no output. Return each query result in operation order. Represent counts as decimal strings.
Function
processRecentEvents(operations: String[], m: int) → String[]Examples
Example 1
operations = ["record 1 a","record 2 b","record 3 a","count 3","top","record 4 c","count 4","top"]m = 3return = ["2","a","2","a"]The first count sees keys a and b before timestamp 3. Recording c evicts the oldest a; the three remaining keys then have equal frequency, so a wins lexicographically.
Example 2
operations = ["top","count 10","record 10 z","count 10","count 11","top"]m = 2return = ["","0","0","1","z"]The initial window is empty. The event at timestamp 10 is excluded from count 10 and included in count 11.
Constraints
1 <= operations.length <= 10^5.1 <= m <= 10^5.- Every operation exactly matches one of the documented forms.
- Record timestamps are integers from
0through10^18and are nondecreasing. - Each key contains from
1through30letters, digits, or underscores.
More Snowflake Phone Screen questions
- Design In-Memory File SystemPHONE SCREEN · Seen Nov 2026
- Merge Two Binary TreesPHONE SCREEN · Seen Nov 2026
- Connect Four from the Last MovePHONE SCREEN · Seen Nov 2026
- Minimum Edits to a Perfect Binary TreePHONE SCREEN · Seen Nov 2026
- Configurable Per-Client Token BucketsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Find Valid SET Card TriplesOA · PHONE SCREEN · Seen Nov 2026
- Minimum Index Distance Between Person and CakeOA · PHONE SCREEN · Seen Nov 2026
- Minimum N-ary Tree Depth DeletionsOA · PHONE SCREEN · Seen Nov 2026
- Sum of Binary Tree Boundary NodesOA · PHONE SCREEN · Seen Nov 2026
- Greedy Person-Cake Matching DistancePHONE SCREEN · Seen Nov 2026
Similar questions at other companies
- Stateful Paginated Fetch NLyft · PHONE SCREENLyft interview questions
- Machine Message RouterOpenAI · PHONE SCREENOpenAI interview questions
- Article Vote TrackerRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- Completions API Rate LimiterCursor · PHONE SCREENCursor interview questions
- Dynamic Least-Loaded Job DispatcherCresta · PHONE SCREENCresta interview questions
- Key-Value Store GET and PUT QPSDatabricks · PHONE SCREENDatabricks interview questions
- Per-Client Sliding-Window Rate LimiterGeneral Motors · OA
- Metric Counter with a Fake ClockSnap Inc. · ONSITE INTERVIEWSnap Inc. interview questions
- Per-Client Sliding-Window Rate LimiterNike, Inc. · ONSITE INTERVIEWNike, Inc. interview questions
- Restaurant Waitlist with Exact-Size TablesGoogle · ONSITE INTERVIEWGoogle interview questions