Google questions

Website Activity Analytics

GooglePhone screenMedium

A website keeps a log of what its users do. You are given the list of records records, where each record is a dictionary with the keys user_id (a string), timestamp (a string in the form YYYY-MM-DD HH:MM:SS) and activity (a string). The records can be in any order.

Implement the class ActivityAnalytics that is built from records.

Part 1: Unique users per activity

Implement unique_users(). It takes no arguments and returns a dictionary that maps each activity that appears in the records to the number of different users who did that activity.

A user counts once per activity even if they performed it many times.

  1. Example 1
    init
    [[{"user_id":"u1","activity":"view","timestamp":"2024-05-01 09:00:00"},{"user_id":"u2","activity":"view","timestamp":"2024-05-01 09:01:00"},{"user_id":"u1","activity":"view","timestamp":"2024-05-01 09:02:00"},{"user_id":"u1","activity":"search","timestamp":"2024-05-01 09:03:00"},{"user_id":"u3","activity":"buy","timestamp":"2024-05-01 09:04:00"},{"user_id":"u2","activity":"search","timestamp":"2024-05-01 09:05:00"},{"user_id":"u3","activity":"view","timestamp":"2024-05-01 09:06:00"},{"user_id":"u3","activity":"view","timestamp":"2024-05-01 09:07:00"}]]
    operations
    [["unique_users",[]]]
    Output
    [{"buy":1,"view":3,"search":2}]

    Why: view was done by u1, u2 and u3 (u1 and u3 more than once, but each counts once), search by u1 and u2, and buy by u3 only.

Constraints

  • 0 <= len(records) <= 10^5
  • 1 <= len(user_id) <= 20
  • 1 <= len(activity) <= 20
  • Every timestamp is a valid date and time
Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question