KatInterview

Sessionize Clickstream Events with a 30-Minute Inactivity Gap

SQL & DataAuto-graded · 14 test cases
EasyAuto-gradedSQL & Datasessionizationwindow-functionsdeduplication

Last updated

Deduplicate retried events, split each user's activity into sessions on gaps over 30 minutes, and summarize each session with its start, end and distinct page count.

Sessionize Clickstream Events

A shopping site logs every page view as a record. Because of network retries, the same event can be logged more than once. You are given these records as a list of dictionaries and must turn them into per-session summaries.

Each record has these string fields:

  • event_id: identifier of the event. Records sharing an event_id are retries of the same event.
  • user_id: the user who viewed the page.
  • page_url: the page that was viewed.
  • timestamp: when the view happened, as an ISO-8601 style string such as "2023-10-01 10:00:00". All times are UTC. A trailing Z may appear.

Task

Implement a function that returns one summary row per session.

  1. Deduplicate. For each event_id, keep only the first record in the input order and discard the later ones.
  2. Order each user's events. Within a user, sort the remaining events by parsed time, then by event_id (plain string comparison) to break ties.
  3. Split into sessions. Walking a user's sorted events, a new session starts at the user's first event. It also starts at any event whose time is strictly more than 30 minutes (1800 seconds) after the immediately preceding event of that user. A gap of exactly 30 minutes stays in the same session. Gaps are measured between consecutive events, so a session may last longer than 30 minutes overall.
  4. Summarize. For every session, produce a dictionary with:
    • user_id: the user.
    • session_id: the session's number for that user, starting at 1 and increasing in time order.
    • session_start_time: the timestamp string of the session's first event, exactly as given in the input.
    • session_end_time: the timestamp string of the session's last event, exactly as given in the input.
    • page_count: the number of distinct page_url values in the session.

Output order

Return the list sorted by user_id in ascending string order. Within each user, sessions appear in ascending session_id order. If the input is empty, return an empty list.

Constraints

  • 0 ≤ number of records ≤ 10^5.
  • Every record contains all four fields, and every timestamp is a valid string. Within one input, all timestamps use the same format.
  • Times are in UTC, so no time-zone conversion is needed.
  • Different users' events never affect each other's sessions.
  • Repeated visits to the same page inside a session count once toward page_count.
  • Duplicates are removed before any gap is computed.

Examples

Example 1

Input: build_session_report([{'event_id': '1', 'user_id': 'A', 'page_url': '/home', 'timestamp': '2023-10-01 10:00:00'}, {'event_id': '2', 'user_id': 'A', 'page_url': '/products', 'timestamp': '2023-10-01 10:15:00'}, {'event_id': '3', 'user_id': 'A', 'page_url': '/home', 'timestamp': '2023-10-01 10:50:00'}])

Output: [{'user_id': 'A', 'session_id': 1, 'session_start_time': '2023-10-01 10:00:00', 'session_end_time': '2023-10-01 10:15:00', 'page_count': 2}, {'user_id': 'A', 'session_id': 2, 'session_start_time': '2023-10-01 10:50:00', 'session_end_time': '2023-10-01 10:50:00', 'page_count': 1}]

Hints

Hint 1

Handle duplicates first: a dictionary keyed by event_id that keeps only the first record seen is enough.

Hint 2

Group the surviving events by user, sort each group by (parsed time, event_id), and compare each event only with the one directly before it. Parse the strings to datetimes so you can compare in seconds.

Hint 3

Open a new session when there is no previous event or the gap is strictly greater than 1800 seconds. Otherwise extend the current session by updating its end and adding the page to a set. Output the original timestamp strings.