User session grouping

Write codeMedium30 minDatabricks and Snowflake style

Your task

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.

Examples

count_sessions(events: list[list], idle: int) -> list[list]
Example 1
Inputcount_sessions([["u1", 0], ["u1", 10], ["u1", 100], ["u2", 5]], 30)
Output[["u1", 2], ["u2", 1]]