PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates a candidate's ability to parse and process large-scale event logs, perform sessionization based on time gaps, handle out-of-order records and memory constraints, and reason about time and space complexity and optimization strategies in the Coding & Algorithms domain.

  • medium
  • Google
  • Coding & Algorithms
  • Software Engineer

Count user sessions from logs

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

Given a large log file where each line is "timestamp,user,action", compute the number of sessions per user. Assume a session ends if the gap between consecutive events for the same user exceeds a configurable timeout T minutes. The file may not be sorted; define the expected input format (e.g., ISO 8601 timestamps), describe how you would parse and group events by user, handle out-of-order records, and process data that does not fit in memory (e.g., streaming or external sort). Provide time and space complexity, discuss optimizations (e.g., indexing, windowing), and outline test cases and edge conditions (empty users, single event, boundary at exactly T, malformed lines).

Quick Answer: This question evaluates a candidate's ability to parse and process large-scale event logs, perform sessionization based on time gaps, handle out-of-order records and memory constraints, and reason about time and space complexity and optimization strategies in the Coding & Algorithms domain.

You are given a list of log lines from a large log file. Each line has the form `"timestamp,user,action"` where `timestamp` is an ISO 8601 datetime (e.g. `2024-01-01T10:00:00`), `user` is a user identifier, and `action` is an arbitrary string. Given the logs and a session timeout `timeout_minutes` (an integer T), compute the number of **sessions per user**. A user's events are grouped into sessions: a new session begins whenever the gap between two consecutive events (for that user, in time order) is **strictly greater than** T minutes. A gap of exactly T minutes keeps the events in the same session. The file may not be sorted, so events for a user can arrive out of order — sort each user's events by timestamp before counting. Skip malformed lines: any line that does not split into at least 3 comma-separated fields, has an empty user, or whose timestamp cannot be parsed as ISO 8601. Return a mapping from each user (that has at least one valid event) to their session count. A user with a single valid event has exactly 1 session. If there are no valid events, return an empty mapping. In the real interview this was framed as a streaming/external-sort problem for data that does not fit in memory; here you implement the in-memory core (group → sort → window) that the external-sort version reduces to.

Constraints

  • 0 <= number of log lines <= 10^6
  • Each well-formed line is "timestamp,user,action" with an ISO 8601 timestamp (e.g. 2024-01-01T10:00:00).
  • 1 <= timeout_minutes <= 1440
  • A new session starts only when the gap is STRICTLY greater than T minutes (exactly T = same session).
  • Malformed lines (fewer than 3 fields, empty user, or unparseable timestamp) are skipped.
  • Users appearing only in malformed lines do not appear in the result.

Examples

Input: (["2024-01-01T10:00:00,alice,click", "2024-01-01T10:20:00,alice,view", "2024-01-01T11:30:00,alice,click"], 30)

Expected Output: {"alice": 2}

Explanation: alice has events at 10:00, 10:20, 11:30. 10:00->10:20 is a 20-min gap (<=30, same session); 10:20->11:30 is a 70-min gap (>30, new session). So 2 sessions.

Input: (["2024-01-01T10:00:00,bob,login", "2024-01-01T10:30:00,bob,logout"], 30)

Expected Output: {"bob": 1}

Explanation: Boundary case: the gap is exactly 30 minutes. Since a new session requires gap STRICTLY greater than T, both events stay in 1 session.

Input: (["2024-01-01T09:00:00,carol,a"], 15)

Expected Output: {"carol": 1}

Explanation: Single event for carol -> exactly 1 session.

Input: ([], 30)

Expected Output: {}

Explanation: No log lines at all -> empty result.

Input: (["2024-01-01T11:30:00,dave,c", "2024-01-01T10:00:00,dave,a", "2024-01-01T10:20:00,dave,b"], 30)

Expected Output: {"dave": 2}

Explanation: Out-of-order input. After sorting: 10:00, 10:20, 11:30. Same as the alice case -> 2 sessions, proving order of arrival doesn't matter.

Input: (["2024-01-01T10:00:00,eve,x", "malformed line", "not,enough", "2024-01-01T10:05:00,,noaction", "2024-01-01T11:00:00,eve,y"], 30)

Expected Output: {"eve": 2}

Explanation: Malformed lines are skipped: 'malformed line' has <3 fields, 'not,enough' has only 2 fields, and the empty-user line is dropped. eve's two valid events at 10:00 and 11:00 are 60 min apart (>30) -> 2 sessions.

Input: (["2024-01-01T10:00:00,frank,a", "2024-01-01T10:10:00,frank,b", "2024-01-01T10:00:00,grace,a", "2024-01-01T12:00:00,grace,b"], 30)

Expected Output: {"frank": 1, "grace": 2}

Explanation: Multiple users counted independently: frank's 10-min gap stays in 1 session; grace's 120-min gap splits into 2 sessions.

Hints

  1. Group events by user first (a hash map of user -> list of timestamps), then sort each user's timestamps. Don't assume the input is sorted.
  2. Count starts at 1 for any user with at least one valid event; increment only when consecutive sorted timestamps differ by MORE than T minutes.
  3. Be careful with the boundary: a gap of exactly T minutes must stay in the same session, so use a strict `>` comparison on the gap in seconds.
  4. For data that does not fit in memory, external-sort the lines by (user, timestamp) and stream them — the per-user counting logic is identical to the in-memory version.
Last updated: Jun 26, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Find A Threshold-Limited Path With Minimum Required Safety - Google (medium)
  • Build Prefix Lookup with a Trie - Google (medium)
  • Deterministic Task Execution Order - Google (easy)
  • Busiest Rental Car - Google (easy)
  • Find Common Free Time Slots Across Calendars - Google (easy)