← Gym/Sessionizer
00:00/ 22 min

🧭 Do not search for the first 15 minutes. When stuck: re-read the requirements → define I/O → choose the data structure → trace a small example by hand → write code.

Implement build_sessions(events, gap), which groups a user event log into sessions.

Input

python
events = [("A", 1), ("A", 2), ("A", 10), ("A", 11), ("B", 3)]

Tuples of (user, timestamp). They are not guaranteed to be sorted — one user's events may arrive interleaved and out of order.

Rules

  • Sort each user's timestamps ascending, then cut them into sessions.
  • Break a session when the gap between two consecutive events is greater than gap (> gap). A gap of exactly gap stays in the same session.
  • Duplicate timestamps each remain their own event.

Output

python
build_sessions(events, gap=5)
# {"A": [[1, 2], [10, 11]], "B": [[3]]}
  • User keys keep their order of first appearance in the input (not sorted time order).
  • Each session is a list of timestamps in ascending order.
  • Empty input returns an empty dictionary.

Level 1 · Splitting into sessions

Implement build_sessions(events, gap). gap is an integer of 0 or more.