Code RoomUser session grouping
MediumPrep Room Coding #1014

User session grouping

CodingDistributed systemsMid–Senior~30 min

You are given `events`, a list of [user, timestamp] pairs (timestamps are integer seconds, NOT necessarily globally sorted, but per user they are non-decreasing in the order given). Group each user's events into sessions: a new session starts whenever the gap from the previous event of that same user exceeds `idle` seconds. Return a list of [user, session_count] sorted by user ascending. A user's first event always starts a session.

Implement
count_sessions(events: list[list], idle: int) → list[list]
Examples
in[[["u1",0],["u1",10],["u1",100],["u2",5]],30]out[["u1",2],["u2",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 30 min
InputExpectedGot
[[["u1",0],["u1",10],["u1",100],["u2",5]],30][["u1",2],["u2",1]]not run yetsample