15-Minute Chat Session Tracker
Learn this problemProblem statement
Implement trackChatSessions. Process the operations in input order. Each operation is one of:
INTERACT userId chatId timestamp: record an interaction.END userId chatId timestamp: record an explicit session end.COUNT userId chatId: output the number of retained interactions for that pair.ACTIVE userId: output the number of active chats for that user.
Event timestamps are integer minutes and may arrive out of order. Let T be the largest event timestamp seen so far. The current window is the inclusive interval [T - 14, T]. When an event advances T, discard expired state. A late event with timestamp below T - 14 is ignored.
A chat is active exactly when its latest retained INTERACT timestamp is in the current window and is strictly greater than its latest END timestamp. A later interaction may reactivate a chat. Return one decimal string for every COUNT or ACTIVE operation, in query order. Queries before any event return 0.
Your retained state must be proportional to events and chats in the current 15-minute window, not the complete history.
Function
trackChatSessions(operations: String[]) → String[]Examples
Example 1
operations = ["INTERACT u1 7 1","INTERACT u1 7 5","COUNT u1 7","ACTIVE u1","END u1 7 6","ACTIVE u1","INTERACT u1 8 20","COUNT u1 7","ACTIVE u1"]return = ["2","1","0","0","1"]The first two interactions are counted while T=5. The end event deactivates chat 7. Advancing to minute 20 expires every event before minute 6, so chat 7 has count zero and only chat 8 is active.
Example 2
operations = ["INTERACT u1 1 30","END u1 1 25","ACTIVE u1","END u1 1 35","INTERACT u1 1 33","ACTIVE u1","INTERACT u1 1 36","ACTIVE u1","COUNT u1 1"]return = ["1","0","1","3"]Late arrivals are interpreted by timestamp. The end at 25 does not kill the interaction at 30. The end at 35 is later than the late interaction at 33, but the interaction at 36 reactivates the chat. All three interactions remain inside [22,36].
Constraints
0 <= operations.length <= 100000- Identifiers are non-empty ASCII tokens without whitespace.
0 <= timestamp <= 10^9.- Every operation has one of the four valid formats.
- For one chat, no two events share a timestamp.