Session Tracker: Sliding Window Chat Analytics
Build an in-memory analytics service that ingests a stream of chat interaction logs and supports efficient sliding-window queries for recent interaction counts, live session tracking, and out-of-order event handling.
Question Timeline
See when this question was last asked and where, including any notes left by other candidates.
Mid November, 2026
Build an in-memory, single-threaded analytics service for chat sessions. It ingests a real-time, globally non-decreasing stream of interaction logs. Each log is user, chat (unique per user), and ts in minutes. Part 1, recent interaction counts: implement ingest(user, chat, ts) and count_recent(user, chat). Count interactions for a (user, chat) in the last 15 minutes, defined as the closed interval [T-14, T], where T is the maximum timestamp observed anywhere in the stream. Events are non-decreasing, no duplicates. Both operations must be efficient at high volume, and memory must stay proportional to chats active inside the window, regardless of total history. The trap is storing everything and filtering at query time. Use a deque of timestamps per (user, chat), plus one global deque in arrival order to drive eviction. Query is then just the deque length, O(1). Part 2, active sessions: events gain a kind, touch or close. A touch is a user sending a message and getting a response. A close is a user explicitly ending the chat, and it may arrive after arbitrary duration, or never. Also implement count_live(user). A session is active if it has a touch within the past 15 minutes and has not received a close after its latest touch. Close is strict: a close at the same timestamp as the latest touch does not end it, and a newer touch clears a previous close and reactivates the session. Part 3, out-of-order events: events may now arrive out of order, with older timestamps possible. Queries still observe non-decreasing T, so cleanup can be deferred to query time. Ingest just records, per key a sorted list of touch timestamps and a max close timestamp, and drops an event already older than T-14. At query time, binary-search away expired touches, then compare latest touch against max close. A late close after the latest touch deactivates retroactively, a late touch newer than the close reactivates, with no replay needed.
Hello Interview Premium
Your account is free and you can post anonymously if you choose.