Katkat

Implement User Sessionization From Event Stream

DiscordSoftware Engineer · Technical Screen

Last updated

This question evaluates proficiency in stream processing, stateful sessionization, time-based aggregation, and per-user metric computation, focusing on competencies such as tracking user activity across unbounded ordered event streams and computing top-channel aggregates.

You are building a backend data-processing component that consumes a stream of newline-delimited JSON events. Each event represents a user sending a message to a channel and has the following shape:

{"event_name":"send_message","timestamp":"2016-11-08T14:09:57Z","user_id":"1","channel_id":"1"}

The input events are guaranteed to arrive in chronological order. There are no missing, duplicated, or corrupt events. However, you must not assume the stream is finite, so your solution cannot depend on logic that runs only after reaching the end of input.

Define a user session as a sequence of events for the same user_id where each event occurs within 30 minutes, inclusive, of that user's previous event. If the gap between two consecutive events for the same user is greater than 30 minutes, the previous session ends and a new session begins.

Implement a program or component that parses the event stream and emits a session record whenever a user session can be finalized. Each emitted session must contain:

  • user_id
  • session_start_ts: timestamp of the first event in the session
  • session_end_ts: timestamp of the last event in the session
  • messages_sent: total number of messages in the session
  • top_channel_id: the channel that received the most messages during the session
  • top_channel_messages_sent: number of messages sent to top_channel_id

The exact output format is flexible as long as all required fields are present. If multiple channels tie for the most messages, document and consistently apply a tie-breaking rule.

Example input lines:

{"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"}

Example output record:

{
 "user_id": "1",
 "session_start_ts": "2016-11-08T14:09:57Z",
 "session_end_ts": "2016-11-08T14:39:57Z",
 "messages_sent": 15,
 "top_channel_id": "3",
 "top_channel_messages_sent": 12
}