Code Room
CodingHardcod-g221
Subject Stream processingLevel Senior–Staff~35 minCommon in Distributed systems interviewsIndustries Software development

Question

Sessionize a stream of user events. Input is a list of (user, ts) tuples already sorted by ts ascending overall, plus an integer 'gap'. For each user independently, consecutive events belong to the same session if the difference between an event's ts and the previous event's ts (for that user) is <= gap; a larger difference starts a new session. Return a list of (user, session_count) tuples for every user that appears, sorted by user ascending. The input may be empty. A user with a single event has 1 session.

Implement
count_sessions(events: list[tuple], gap: int) → list[tuple]
Examples
in[[["a",1],["a",3],["a",10],["b",2]],5]out[["a",2],["b",1]]
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.