KatInterview

Event Log Analysis: Distinct Users and Session Duration

Arrays & HashingAuto-graded · 17 test cases
MediumAuto-gradedArrays & Hashinggroupingaggregationtimestamps

Last updated

Tests grouping, deduplication, and timestamp arithmetic over semi-structured event records.

Event Log Analysis

You receive a list of event records from a web application. Each record is a dictionary with three fields:

  • user_id (int): the identifier of the user who triggered the event.
  • timestamp (str): when the event occurred, in the format 'YYYY-MM-DD HH:MM:SS' (24-hour notation, single time zone, no offset markers).
  • activity (str): a label describing the event type, such as 'login', 'view', 'purchase', or 'logout'.

Your function receives this list and must return a dictionary containing two computed metrics:

{
    "unique_users_per_activity": { ... },
    "average_time_per_user_seconds": ...
}

Metric 1: Unique users per activity

Build a mapping where each key is an activity label and each value is the number of distinct users who performed that activity at least once. If a single user performs the same activity multiple times, count that user only once for that activity.

Metric 2: Average session duration per user

For each user, compute their session duration as the difference in seconds between their earliest and latest recorded events. A user who appears in only one record has a duration of 0 seconds.

Return the mean of these per-user durations across all distinct users, as a float (in seconds).

Example

Given:

[
    {'user_id': 1, 'timestamp': '2024-07-26 10:00:00', 'activity': 'login'},
    {'user_id': 2, 'timestamp': '2024-07-26 10:05:00', 'activity': 'login'},
    {'user_id': 1, 'timestamp': '2024-07-26 10:10:00', 'activity': 'view'},
    {'user_id': 1, 'timestamp': '2024-07-26 10:15:00', 'activity': 'purchase'},
    {'user_id': 2, 'timestamp': '2024-07-26 10:20:00', 'activity': 'view'},
    {'user_id': 3, 'timestamp': '2024-07-26 10:25:00', 'activity': 'login'},
    {'user_id': 1, 'timestamp': '2024-07-26 10:30:00', 'activity': 'logout'},
]

Metric 1: login was performed by users 1, 2, and 3; view by users 1 and 2; purchase by user 1; logout by user 1.

{'login': 3, 'view': 2, 'purchase': 1, 'logout': 1}

Metric 2:

  • User 1: earliest at 10:00:00, latest at 10:30:00 → 1800 seconds
  • User 2: earliest at 10:05:00, latest at 10:20:00 → 900 seconds
  • User 3: single event → 0 seconds

Average: (1800 + 900 + 0) / 3 = 900.0

Return:

{
    "unique_users_per_activity": {'login': 3, 'view': 2, 'purchase': 1, 'logout': 1},
    "average_time_per_user_seconds": 900.0
}

Constraints

  • 0 ≤ len(user_activity) ≤ 10⁵
  • Records may arrive in any order (not sorted by timestamp).
  • user_id is a positive integer.
  • All timestamps are valid and follow the format 'YYYY-MM-DD HH:MM:SS'.
  • Activity labels are non-empty strings.
  • When the input is empty, return {"unique_users_per_activity": {}, "average_time_per_user_seconds": 0.0}.
  • A user with exactly one event contributes 0 to the numerator and 1 to the denominator of the average.

Examples

Example 1

Input: user_activity_analytics([{'user_id': 1, 'timestamp': '2024-07-26 10:00:00', 'activity': 'login'}, {'user_id': 2, 'timestamp': '2024-07-26 10:05:00', 'activity': 'login'}, {'user_id': 1, 'timestamp': '2024-07-26 10:10:00', 'activity': 'view'}, {'user_id': 1, 'timestamp': '2024-07-26 10:15:00', 'activity': 'purchase'}, {'user_id': 2, 'timestamp': '2024-07-26 10:20:00', 'activity': 'view'}, {'user_id': 3, 'timestamp': '2024-07-26 10:25:00', 'activity': 'login'}, {'user_id': 1, 'timestamp': '2024-07-26 10:30:00', 'activity': 'logout'}])

Output: {'unique_users_per_activity': {'login': 3, 'view': 2, 'purchase': 1, 'logout': 1}, 'average_time_per_user_seconds': 900}

Example 2

Input: user_activity_analytics([])

Output: {'unique_users_per_activity': {}, 'average_time_per_user_seconds': 0}

Example 3

Input: user_activity_analytics([{'user_id': 7, 'timestamp': '2024-01-02 12:00:00', 'activity': 'view'}, {'user_id': 7, 'timestamp': '2024-01-02 11:00:00', 'activity': 'view'}, {'user_id': 8, 'timestamp': '2024-01-02 11:30:00', 'activity': 'login'}, {'user_id': 7, 'timestamp': '2024-01-02 12:00:00', 'activity': 'view'}])

Output: {'unique_users_per_activity': {'view': 1, 'login': 1}, 'average_time_per_user_seconds': 1800}

Hints

Hint 1

Use a set for each activity to automatically deduplicate user IDs as you scan the records.

Hint 2

Track the minimum and maximum timestamp for each user to compute their session duration without sorting.

Hint 3

Parse timestamps with datetime.strptime and use .total_seconds() on the difference to get a float duration.