Message Event Aggregation
Updated
Problem statement
This exercise combines the first three parts of a message-event aggregation task.
You are given a list of chat events and a list of query timestamps. Each event is encoded as a comma-separated string:
timestamp,userId,chatId,eventTypeeventType is one of:
message: a message sent in the chat.react: the user reacted in the chat, making the chat active for that user unless a laterend_chatevent ends it.end_chat: the user ended the chat.
For each query time q, consider events in the inclusive five-minute window [q - 300, q]. Timestamps are integers measured in seconds.
For every user with at least one event in the window, compute:
messageCount: the number ofmessageevents for that user in the window.activeChatCount: the number of distinct chats whose latestreactorend_chatevent in the window isreact. Amessageevent does not change a chat's active state.
Events may be given out of timestamp order. Process them by timestamp; when timestamps are equal, preserve their original input order.
Return one string for each query, in the original query order. Format a user's result as userId:messageCount:activeChatCount, sort users lexicographically by userId, and join entries with semicolons. If the window contains no events, return an empty string for that query.
Memory requirement
As the five-minute window advances, discard expired aggregation state. Apart from storage used to order the input and hold the output, retained counting and chat-state data must be proportional to the events and user-chat pairs in the current window, not to every chat seen earlier.
Function
aggregateMessageEvents(events: String[], queryTimes: int[]) → String[]Examples
Example 1
events = ["10,u1,c1,message", "20,u1,c1,react", "80,u2,c5,message", "90,u2,c5,react", "200,u1,c1,end_chat", "260,u1,c2,react", "400,u1,c2,message"]queryTimes = [260, 500]return = ["u1:1:1;u2:1:1", "u1:1:1"]At time 260, each user has one message in the window. Chat c2 is active for u1, while c5 is active for u2. At time 500, the window starts at 200; only u1 has events in the window, with one message and one active chat.
Example 2
events = ["300,u2,c8,end_chat", "40,u1,c1,react", "100,u1,c1,end_chat", "70,u1,c2,react", "20,u2,c8,react", "60,u1,c2,message", "110,u2,c8,message"]queryTimes = [120, 320]return = ["u1:1:1;u2:1:1", "u1:1:1;u2:1:0"]The events are not given in timestamp order. At time 120, chat c8 is active for u2. At time 320, the later end_chat event makes it inactive. Chat c2 remains active for u1 at both query times.
Constraints
0 <= events.length <= 10^50 <= queryTimes.length <= 10^50 <= timestamp, q <= 10^9- Every event has exactly four comma-separated fields and a valid
eventType. userIdandchatIdcontain no commas, colons, or semicolons.
More OpenAI Onsite Interview questions
- Sparse Plant Infection SimulationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Rectangle Point Coverage CountsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 15-Minute Chat Session TrackerPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Segmented Durable Key-Value StorePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Durable Work Queue OperationsPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Versioned Social NetworkPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Budgeted API Inference with Out-of-Order FeedbackONSITE INTERVIEW · Seen Oct 2026
- Plant Infection Simulation, Part 4: Death CountdownPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Count Friend CirclesPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Incremental Hard Attention with a KV CachePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Merchant Payout Scheduler, Part 2: Per-Merchant SchedulesStripe · PHONE SCREENStripe interview questions
- Count Zero Request ServersSalesforce · ONSITE INTERVIEWSalesforce interview questions
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- Top K Best-Selling Products in the Last N DaysAmazon · ONSITE INTERVIEW
- Pins Violation Log QueriesPinterest · PHONE SCREENPinterest interview questions
- Camel CardsRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- Shopping Cart ReceiptSony Interactive Entertainment PlayStation · PHONE SCREEN
- Banking System with Top ActivityCapital One · ONSITE INTERVIEWCapital One interview questions
- Linked Perception HistoryWaymo · ONSITE INTERVIEWWaymo interview questions
- Banking System with Pending Transfer AcceptanceCircle · PHONE SCREENCircle interview questions