KatInterview

Group Activity Logs into Time-Based Sessions

Arrays & HashingAuto-graded · 14 test cases
HardAuto-gradedArrays & Hashingtime-seriesgroupingdatetime

Last updated

Tests time-series grouping, datetime arithmetic, gap-based segmentation, and aggregation with distinct counts.

You are given a list of activity log records. Each record is a dictionary with three fields:

  • user_id: a string identifying the user
  • log_datetime: a timestamp string in format "YYYY-MM-DD HH:MM:SS"
  • topic: a string representing the activity category

Your task is to group these logs into sessions for each user, where a session is a sequence of consecutive events with no gap exceeding 30 minutes between adjacent events.

Session Rules

  1. Sort all records by user ID, then by timestamp.
  2. For each user, partition their events into sessions:
    • A new session begins when the time gap from the previous event is strictly greater than 30 minutes, or when the user changes.
    • Events exactly 30 minutes apart remain in the same session.
  3. For each session, compute:
    • user_id: the user identifier
    • session_start: the timestamp of the first event in the session (as a string)
    • session_end:
      • If the session contains two or more events, use the timestamp of the last event
      • If the session contains exactly one event, set session_end = session_start + 30 minutes
    • count_topic: the number of distinct topics appearing in the session
    • count_time: the total number of events (records) in the session

Return a list of dictionaries, one per session, in the order sessions are encountered after sorting.

Example

Input:

[
  {"user_id": "001", "log_datetime": "2025-03-01 00:01:00", "topic": "pricing"},
  {"user_id": "001", "log_datetime": "2025-03-01 00:02:00", "topic": "hotel"},
  {"user_id": "001", "log_datetime": "2025-03-01 00:03:00", "topic": "pricing"},
  {"user_id": "001", "log_datetime": "2025-03-01 01:30:00", "topic": "restaurant"},
  {"user_id": "001", "log_datetime": "2025-03-01 02:30:00", "topic": "restaurant"}
]

Output:

[
  {"user_id": "001", "session_start": "2025-03-01 00:01:00", "session_end": "2025-03-01 00:03:00", "count_topic": 2, "count_time": 3},
  {"user_id": "001", "session_start": "2025-03-01 01:30:00", "session_end": "2025-03-01 02:00:00", "count_topic": 1, "count_time": 1},
  {"user_id": "001", "session_start": "2025-03-01 02:30:00", "session_end": "2025-03-01 03:00:00", "count_topic": 1, "count_time": 1}
]

Explanation:

  • Events at 00:01, 00:02, and 00:03 form one session (gaps of 1 minute each). Two distinct topics: "pricing" and "hotel".
  • Event at 01:30 starts a new session (87-minute gap from 00:03). Single event, so end time is 01:30 + 30 minutes = 02:00.
  • Event at 02:30 starts a new session (60-minute gap from 01:30). Single event, so end time is 03:00.

Constraints

  • 0 ≤ number of records ≤ 100,000
  • All timestamps are valid and in format "YYYY-MM-DD HH:MM:SS"
  • user_id and topic are non-empty strings
  • Records may arrive in any order; you must sort them
  • Multiple users may be present in the input
  • A user may have multiple sessions

Examples

Example 1

Input: group_activity_sessions([{'user_id': '001', 'log_datetime': '2025-03-01 00:01:00', 'topic': 'pricing'}, {'user_id': '001', 'log_datetime': '2025-03-01 00:02:00', 'topic': 'hotel'}, {'user_id': '001', 'log_datetime': '2025-03-01 00:03:00', 'topic': 'pricing'}, {'user_id': '001', 'log_datetime': '2025-03-01 01:30:00', 'topic': 'restaurant'}, {'user_id': '001', 'log_datetime': '2025-03-01 02:30:00', 'topic': 'restaurant'}])

Output: [{'user_id': '001', 'session_start': '2025-03-01 00:01:00', 'session_end': '2025-03-01 00:03:00', 'count_topic': 2, 'count_time': 3}, {'user_id': '001', 'session_start': '2025-03-01 01:30:00', 'session_end': '2025-03-01 02:00:00', 'count_topic': 1, 'count_time': 1}, {'user_id': '001', 'session_start': '2025-03-01 02:30:00', 'session_end': '2025-03-01 03:00:00', 'count_topic': 1, 'count_time': 1}]

Hints

Hint 1

Sort all records by (user_id, log_datetime) first, then process them in a single linear pass.

Hint 2

Track the previous event's timestamp. When the gap exceeds 30 minutes or the user changes, close the current session and start a new one.

Hint 3

Don't forget to finalize the last session after the loop ends—this is a common source of bugs.