Code RoomSessionize user events
HardPrep Room Coding #795

Sessionize user events

CodingDistributed systemsSenior–Staff~35 min

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.

0:00 of about 35 min
InputExpectedGot
[[["a",1],["a",3],["a",10],["b",2]],5][["a",2],["b",1]]not run yetsample