Unique users per activity and average time on site
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Google's first round (coding), last asked May 2025. It tests python, data processing and sql.
You get a list of records, each one user's action on a website: user_id, a timestamp string "YYYY-MM-DD HH:MM:SS" in one time zone, and an activity such as login, view, purchase or logout. Records may arrive in any order. Compute two things: how many distinct users did each activity, and the average time users spent on the site.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
unique_users_per_activity(records: list[dict]) -> dict[str, int]— per activity, the number of distinct users who did it at least once;{}for no records. -
average_time_on_site(records: list[dict]) -> float— a user's time on site is the seconds from their earliest to their latest record (0 for a user with one record); return the average over all distinct users,0.0for no records.
Example
records = [
{"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"},
]
unique_users_per_activity(records) -> {"login": 3, "view": 2, "purchase": 1, "logout": 1}
average_time_on_site(records) -> 900.0 # user 1: 1800 s, user 2: 900 s, user 3: 0 s
Constraints
- Up to millions of records; parse timestamps yourself and don't assume they are sorted.
- The same
(user_id, activity)may repeat; count the user once for it.
Clarifying questions
- Is "time on site" first to last event, or should it be split into sessions by an idle gap?
- Do users with a single event count in the average (with 0)?
- What if a timestamp is malformed or missing?
- Are all timestamps in one time zone?
What a strong answer covers
- One pass: a set of users per activity, and the minimum and maximum timestamp per user. O(N) time, O(users + activity-user pairs) space; no sorting needed.
- Parsing with
datetime.strptimeand differences in whole seconds. - Edge cases: empty input (no division by zero), single-event users, out-of-order and duplicate records, malformed rows skipped and counted.
- The SQL version:
COUNT(DISTINCT user_id) ... GROUP BY activity, andAVGover a per-user subquery ofMAX(ts) - MIN(ts). - Tests for each rule.
Follow-ups
- Split each user's time into sessions with a 30-minute idle timeout and average session length instead.
- Count only sessions that end in a
logout, and handle sessions with no logout. - The records stream in continuously; keep both answers current.
Where it's asked
- Google · First round: coding · Last asked May 202545 min · Coding in a Google Doc, no syntax highlighting or autocomplete · No AI
What it tests
python · data processing · sql
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
