Split a User Event Stream Into Inactivity-Based Sessions

Last updated: November 29, 2026

Quick Overview

Given raw events as (distinct_id, timestamp, event_name), group each user's events into sessions where a gap longer than 30 minutes starts a new session, and return each session's start, end and event count.

Mixpanel
Coding & Algorithms
Software Engineer
Mixpanel
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

0

0 solved


Given raw events as (distinct_id, timestamp, event_name), group each user's events into sessions where a gap longer than 30 minutes starts a new session, and return each session's start, end and event count.

Mixpanel computes session-based metrics (session length, sessions per user) on top of an event stream that arrives out of order. A phone screen version checks whether you group by user, sort before scanning, and handle the boundary condition on the timeout cleanly.

What the Interviewer Expects
  • Group events per user before computing gaps
  • Sort timestamps because ingestion order is not event order
  • Define the boundary (gap equal to timeout stays in the session) explicitly
  • Single linear scan per user after sorting
  • Return a clear structure per user and state complexity
Key Topics to Cover
sessionization
sorting
grouping
event streams
boundary conditions
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you also end a session at local midnight in the project's timezone?
  • Events stream in continuously: how do you emit sessions incrementally and handle a late event that bridges two sessions?
  • How would you compute median session length across all users without holding every session in memory?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input is a list of (distinct_id, ts, name) with ts in seconds, possibly unordered. Output maps each user to a list of [start, end, count]. A new session starts when the gap from the previous eve...

High-Level Design
  1. Bucket timestamps by user in a dict.
  2. Sort each user's timestamps.
  3. Walk them once, keeping start, prev and count. When t - prev > timeout, close the current session and open a new one ...

Submit Your Answer
Markdown supported

Related Questions