PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates a candidate's ability to perform event-log sessionization and time-interval aggregation across users and spaces, testing skills in algorithms, state management, and event processing within the Coding & Algorithms domain.

  • medium
  • xAI
  • Coding & Algorithms
  • Software Engineer

Compute total active time per Twitter Space

Company: xAI

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

You are given an event log of user activity in Twitter Spaces. Each record has: - `operation`: one of `create`, `join`, `leave` - `space_id`: identifier of the space (string) - `user_id`: identifier of the user (string) - `timestamp`: Unix timestamp in seconds (integer) A user is considered **in a space** from the time they `join` until they `leave`. The `create` operation means the user created the space and is **also considered to have joined at that timestamp**. ### Task Return the **total active time (in seconds)** for each space, defined as: > sum over all users of (time that user spent in that space) A user may join/leave the same space multiple times; all sessions should be summed. ### Example Input records: - `["create", "abc", "user_1", 1234567000]` - `["join", "abc", "user_2", 1234567100]` - `["leave", "abc", "user_2", 1234567300]` - `["create", "def", "user_2", 1234568000]` - `["leave", "def", "user_2", 1234568500]` - `["leave", "abc", "user_1", 1234569000]` Output: - `{ "abc": 2200, "def": 500 }` Explanation: `user_1` spent `2000` seconds in `abc`, `user_2` spent `200` seconds in `abc`, and `user_2` spent `500` seconds in `def`. ### Follow-up (real-time) Design a data structure / approach that can continuously output the **top-k spaces by current active user count** (i.e., number of users currently in the space), with updates arriving as join/leave events stream in.

Quick Answer: This question evaluates a candidate's ability to perform event-log sessionization and time-interval aggregation across users and spaces, testing skills in algorithms, state management, and event processing within the Coding & Algorithms domain.

Part 1: Compute Total Active Time per Twitter Space

You are given a chronological event log of user activity in Twitter Spaces. Each record contains an operation, a space_id, a user_id, and a Unix timestamp in seconds. A user is active in a space from a join event until a later leave event. A create event also counts as the creator joining the space at that timestamp. Return the total completed active time for each space, summed across all users and all sessions. Include every space that appears in the records, even if its total is 0. If a leave has no matching active session, ignore it. If a session is still active after the final record, it contributes 0 because there is no closing timestamp.

Constraints

  • 0 <= len(records) <= 200000
  • operation is one of 'create', 'join', or 'leave'
  • 1 <= len(space_id), len(user_id) <= 100
  • 0 <= timestamp <= 10^18
  • records are provided in nondecreasing timestamp order
  • A user may have multiple non-overlapping sessions in the same space

Examples

Input: ([['create', 'abc', 'user_1', 1234567000], ['join', 'abc', 'user_2', 1234567100], ['leave', 'abc', 'user_2', 1234567300], ['create', 'def', 'user_2', 1234568000], ['leave', 'def', 'user_2', 1234568500], ['leave', 'abc', 'user_1', 1234569000]],)

Expected Output: {'abc': 2200, 'def': 500}

Explanation: user_1 spends 2000 seconds in abc, user_2 spends 200 seconds in abc, and user_2 spends 500 seconds in def.

Input: ([],)

Expected Output: {}

Explanation: There are no records, so there are no spaces to report.

Input: ([['create', 's1', 'u1', 0], ['leave', 's1', 'u1', 10], ['join', 's1', 'u1', 20], ['leave', 's1', 'u1', 25], ['join', 's1', 'u2', 30], ['leave', 's1', 'u2', 40]],)

Expected Output: {'s1': 25}

Explanation: The completed sessions last 10, 5, and 10 seconds, for a total of 25.

Input: ([['create', 's1', 'u1', 100], ['join', 's1', 'u2', 110], ['leave', 's1', 'u1', 160], ['leave', 's1', 'u2', 180], ['create', 's2', 'u1', 200], ['leave', 's2', 'u1', 200]],)

Expected Output: {'s1': 130, 's2': 0}

Explanation: s1 has overlapping users, so their times are summed: 60 + 70 = 130. s2 has a zero-length session.

Input: ([['leave', 'ghost', 'u1', 5], ['create', 'lonely', 'u2', 10]],)

Expected Output: {'ghost': 0, 'lonely': 0}

Explanation: The unmatched leave is ignored, and the still-open create session has no closing timestamp.

Hints

  1. Track currently active sessions by the pair (space_id, user_id).
  2. When a leave event arrives, the elapsed time is leave_timestamp - stored_join_timestamp.

Part 2: Real-Time Top-K Spaces by Active User Count

You are given a stream of Twitter Spaces events. After each event, output the current top k spaces by active user count. A user is active in a space after a create or join event and stops being active after a leave event. A create event counts as the creator joining the space. Duplicate create or join events for a user who is already active in that same space do not increase the count. A leave event for a user who is not currently active in that space has no effect. The top k spaces should be sorted by active count descending, with ties broken by lexicographically smaller space_id first. Only spaces with positive active count should appear in a snapshot.

Constraints

  • 0 <= len(events) <= 200000
  • 0 <= k <= 1000
  • operation is one of 'create', 'join', or 'leave'
  • 1 <= len(space_id), len(user_id) <= 100
  • A user may be active in multiple different spaces at the same time
  • For one user-space pair, duplicate join/create events while already active do not change the active count

Examples

Input: ([['create', 'abc', 'u1'], ['join', 'abc', 'u2'], ['create', 'def', 'u2'], ['leave', 'abc', 'u1'], ['leave', 'abc', 'u2']], 2)

Expected Output: [[['abc', 1]], [['abc', 2]], [['abc', 2], ['def', 1]], [['abc', 1], ['def', 1]], [['def', 1]]]

Explanation: The snapshots show the top 2 active spaces after each event. Ties are ordered by space_id.

Input: ([], 3)

Expected Output: []

Explanation: With no events, there are no snapshots.

Input: ([['join', 'b', 'u1'], ['join', 'a', 'u2'], ['join', 'b', 'u3'], ['leave', 'b', 'u1']], 1)

Expected Output: [[['b', 1]], [['a', 1]], [['b', 2]], [['a', 1]]]

Explanation: When a and b both have count 1, a wins the lexicographic tie-break.

Input: ([['join', 'x', 'u1'], ['join', 'x', 'u1'], ['leave', 'x', 'u2'], ['leave', 'x', 'u1'], ['leave', 'x', 'u1']], 3)

Expected Output: [[['x', 1]], [['x', 1]], [['x', 1]], [], []]

Explanation: The duplicate join and invalid leaves do not change the count.

Input: ([['join', 'a', 'u1'], ['join', 'b', 'u2']], 0)

Expected Output: [[], []]

Explanation: When k is 0, each snapshot is empty, though the stream is still processed.

Hints

  1. Maintain a set of active (space_id, user_id) pairs and a count per space.
  2. A heap can support top-k queries, but counts change over time, so use lazy deletion with version numbers to ignore stale heap entries.
Last updated: Jun 13, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ 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
  • 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

  • Flatten and unflatten nested Python structures - xAI (nan)
  • Compute dasher pay from order events - xAI (nan)
  • Design a Recoverable Iterator - xAI (medium)
  • Implement Distributed Matrix Multiplication - xAI (hard)
  • Find kth element and sliding-window kth in stream - xAI (hard)