← Questions

Count user sessions from an activity log

0
GoogleCodingLast asked Sep 2025

A coding interview question asked in Google's onsite coding round, last asked Sep 2025. It tests logs, hash maps and sliding window.

A large log file has one event per line: timestamp,user,action, with ISO 8601 timestamps, in no particular order. A user's session is a run of their events where no gap between consecutive events exceeds a timeout of T minutes. Return the number of sessions per user, then explain how you would process a file that doesn't fit in memory.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • count_sessions(lines: Iterable[str], timeout_minutes: int) -> dict[str, int] — sort each user's events by time; a new session starts when the gap to the previous event is strictly greater than T minutes, so a gap of exactly T stays in the same session. Skip malformed lines: fewer than three fields, an empty user, or a timestamp that doesn't parse. A user with one valid event has one session; users seen only in malformed lines are absent.

Example

count_sessions(["2024-01-01T10:00:00,alice,click",
                "2024-01-01T10:20:00,alice,view",
                "2024-01-01T11:30:00,alice,click"], 30)   ->  {"alice": 2}   # 20-minute gap, then 70

count_sessions(["2024-01-01T10:00:00,bob,login",
                "2024-01-01T10:30:00,bob,logout"], 30)   ->  {"bob": 1}     # exactly 30 minutes

count_sessions(["2024-01-01T11:00:00,cy,view",
                "2024-01-01T10:00:00,cy,click",
                "not a line",
                "2024-01-01T10:45:00,cy,view",
                "2024-01-01T25:00:00,dee,view"], 30)     ->  {"cy": 2}      # sorted: 10:00 | 10:45, 11:00

Constraints

  • Up to 10^6 lines in memory for the core version; far more for the follow-up.
  • 1 <= T <= 1440.

Clarifying questions

  • Is a gap of exactly T a new session?
  • What timestamp format, and are time zones mixed?
  • Should malformed lines be skipped, counted or fail the run?
  • Do actions matter (for example, does logout end a session)?

What a strong answer covers

  • Group by user into lists of timestamps, sort each, count gaps over T. O(N log N) time, O(N) space.
  • Parsing defensively: split with a limit so commas in the action don't break it, parse with the standard library, and count what was skipped.
  • The boundary at exactly T, a single event, out-of-order lines and duplicate timestamps.
  • Too big for memory: an external sort by (user, timestamp) then one streaming pass that keeps only the previous timestamp per user; or hash-partition by user into files that fit, then process each. A MapReduce form (map by user, reduce sorts and counts).
  • Tests for each rule above.

Follow-ups

  • Events arrive as a live stream, a little out of order. Emit sessions as they close (watermarks, allowed lateness).
  • Return each session's start, end and length, not only the count.
  • Report the average session length per user per day.

Where it's asked

  • Google · Onsite coding · Last asked Sep 202545 min · Usually two rounds, mostly in person: whiteboard plus a Chromebook with Google Docs · No AI (a pilot code-comprehension round lets candidates use Gemini)

What it tests

logs · hash maps · sliding window

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding