KatInterview

Stateful Activity Window Aggregation Over an Event Stream

Data Structure DesignAuto-graded · 14 test cases
HardAuto-gradedData Structure Designhash-mapsimulationstreaming

Last updated

Tests whether a candidate can maintain per-user rolling time windows over a chronologically ordered stream and emit aggregated records incrementally without buffering the whole input.

You are building a pipeline stage that processes a chronologically ordered stream of chat activity records. Each record is a dictionary with the following fields:

  • event_name — always "send_message"
  • timestamp — an ISO 8601 string in UTC, e.g. "2024-03-15T09:22:00Z"
  • user_id — a string identifying the sender
  • channel_id — a string identifying the destination channel

Records arrive in non-decreasing timestamp order. You may not assume the stream is finite, so your logic must never depend on reaching the end of the input. Process each record as it arrives and emit completed windows eagerly.

Activity Windows

For each user, define an activity window as a maximal sequence of that user's records where every consecutive pair is separated by 30 minutes or less. Formally:

  • If the gap between a user's new record and their most recent previous record is strictly greater than 30 minutes, the previous window is closed and a new one begins.
  • A gap of exactly 30 minutes keeps the new record in the same window.

A window is finalized — and must be emitted — the moment a later record from the same user proves the window is closed (i.e., the gap exceeds 30 minutes). Windows that are still open when the input ends are not emitted.

Output Format

Return a list of window records in the order they are finalized. Each record must be a dictionary with exactly these keys:

KeyTypeDescription
user_idstrThe user this window belongs to
session_start_tsstrTimestamp string of the window's first record
session_end_tsstrTimestamp string of the window's last record
messages_sentintTotal number of records in the window
top_channel_idstrChannel with the highest record count in the window
top_channel_messages_sentintRecord count for top_channel_id

Tie-breaking rule: when two or more channels share the highest count, pick the one whose channel_id string is lexicographically smallest. String comparison applies, so "10" sorts before "9".

Constraints

  • 1 ≤ len(events) ≤ 10^6
  • All timestamps are valid ISO 8601 UTC strings ending in Z
  • user_id and channel_id are non-empty strings
  • Records are guaranteed to be in non-decreasing timestamp order
  • There are no missing or malformed fields
  • A user may appear many times or only once
  • Multiple users may be active concurrently; their windows are fully independent

Examples

Example 1

Input: sessionize_events([{'event_name': 'send_message', 'timestamp': '2016-11-08T14:09:57Z', 'user_id': '1', 'channel_id': '1'}, {'event_name': 'send_message', 'timestamp': '2016-11-08T14:10:01Z', 'user_id': '1', 'channel_id': '1'}, {'event_name': 'send_message', 'timestamp': '2016-11-08T14:10:07Z', 'user_id': '2', 'channel_id': '1'}, {'event_name': 'send_message', 'timestamp': '2016-11-08T14:40:08Z', 'user_id': '1', 'channel_id': '3'}])

Output: [{'user_id': '1', 'session_start_ts': '2016-11-08T14:09:57Z', 'session_end_ts': '2016-11-08T14:10:01Z', 'messages_sent': 2, 'top_channel_id': '1', 'top_channel_messages_sent': 2}]

Example 2

Input: sessionize_events([{'event_name': 'send_message', 'timestamp': '2024-01-01T00:00:00Z', 'user_id': 'u', 'channel_id': 'b'}, {'event_name': 'send_message', 'timestamp': '2024-01-01T00:30:00Z', 'user_id': 'u', 'channel_id': 'a'}, {'event_name': 'send_message', 'timestamp': '2024-01-01T01:00:01Z', 'user_id': 'u', 'channel_id': 'c'}])

Output: [{'user_id': 'u', 'session_start_ts': '2024-01-01T00:00:00Z', 'session_end_ts': '2024-01-01T00:30:00Z', 'messages_sent': 2, 'top_channel_id': 'a', 'top_channel_messages_sent': 1}]

Hints

Hint 1

For each user you only need to remember their current open window — not every past event. A dictionary keyed by user_id is the right structure.

Hint 2

You can decide whether to close a window the moment the *next* event from that user arrives. At that point, and only at that point, do you have enough information to know the previous window is final.

Hint 3

To pick the top channel under the tie-breaking rule, sort (or use min) with a key that ranks by descending count first and ascending channel string second: (-count, channel_id).