Total active time per Twitter Space from an unsorted event log

Quick Overview

Compute the total active time of each live audio Space from an unsorted log of create, join and leave events, where a Space stays active while anyone is in it. It tests event sorting, per-key state tracking with sets, and careful handling of rejoins and Spaces still open at the end of the log.

Total active time per Twitter Space from an unsorted event log

Company: X (Twitter)

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given an event log of create, join and leave events for Twitter Spaces, compute the total active time of each Space. A Space becomes active when it is created and becomes inactive when all participants have left. The creator counts as a participant from the moment of creation. Each event is `[space_id, user_id, event_type, timestamp]`, where `event_type` is `"create"`, `"join"` or `"leave"` and `timestamp` is a non-negative integer written as a decimal string. A Space's active time runs from its creation until its last participant leaves. If a Space still has participants at the end of the log, its active time runs up to the maximum timestamp in the entire log. Return a dictionary mapping each `space_id` to its total active time. ### Function Signature ```python def space_active_time(events: list[list[str]]) -> dict[str, int]: ``` ### Rules - Process events in increasing numeric order of timestamp. Events with equal timestamps are processed in the order they appear in `events`. - The participants of a Space form a set. Joining a Space you are already in changes nothing, and a leave removes that user. - If a user joins a Space that currently has no participants, a new active period starts at that join. The total active time is the sum of all active periods, so time while a Space is empty does not count. - Every Space that has a create event appears in the output, even when its total is 0. Key order in the returned dictionary does not matter. ### Constraints - `1 <= len(events) <= 10000` - `space_id` and `user_id` are non-empty strings - `event_type` is one of `"create"`, `"join"` or `"leave"` - `0 <= int(timestamp) <= 10**9` - Events may not be sorted by timestamp. - Each Space is created exactly once, and its create event is processed before any join or leave for that Space. - A leave event always names a user who is currently in that Space. ### Examples ```text Input: events = [ ["space1", "alice", "create", "0"], ["space1", "bob", "join", "5"], ["space1", "alice", "leave", "10"], ["space1", "bob", "leave", "15"] ] Output: {"space1": 15} ``` Alice creates the Space at t = 0 and Bob joins at 5. Alice leaves at 10 while Bob is still present, and Bob's leave at 15 empties the Space, so the active time is 15 - 0 = 15. ```text Input: events = [ ["spaceA", "user1", "create", "10"], ["spaceB", "user2", "create", "20"], ["spaceA", "user3", "join", "25"], ["spaceB", "user2", "leave", "30"], ["spaceA", "user1", "leave", "35"], ["spaceA", "user3", "leave", "40"] ] Output: {"spaceA": 30, "spaceB": 10} ``` spaceA is active from its creation at 10 until user3 leaves at 40, which is 30. spaceB is active from 20 until user2 leaves at 30, which is 10. ```text Input: events = [ ["s2", "u2", "create", "3"], ["s1", "u1", "leave", "4"], ["s1", "u1", "create", "0"], ["s1", "u3", "join", "6"], ["s1", "u3", "leave", "9"], ["s2", "u5", "join", "7"] ] Output: {"s1": 7, "s2": 6} ``` In time order, s1 is active from 0 to 4, empty until u3 joins at 6, and active again until 9, for 4 + 3 = 7. s2 still has u2 and u5 at the end, so it counts up to the log's maximum timestamp 9, giving 9 - 3 = 6.

Overview: Compute the total active time of each live audio Space from an unsorted log of create, join and leave events, where a Space stays active while anyone is in it. It tests event sorting, per-key state tracking with sets, and careful handling of rejoins and Spaces still open at the end of the log.

X (Twitter) logo
X (Twitter)
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given an event log of create, join and leave events for Twitter Spaces, compute the total active time of each Space.

A Space becomes active when it is created and becomes inactive when all participants have left. The creator counts as a participant from the moment of creation. Each event is [space_id, user_id, event_type, timestamp], where event_type is "create", "join" or "leave" and timestamp is a non-negative integer written as a decimal string.

A Space's active time runs from its creation until its last participant leaves. If a Space still has participants at the end of the log, its active time runs up to the maximum timestamp in the entire log. Return a dictionary mapping each space_id to its total active time.

Function Signature

def space_active_time(events: list[list[str]]) -> dict[str, int]:

Rules

  • Process events in increasing numeric order of timestamp. Events with equal timestamps are processed in the order they appear in events .
  • The participants of a Space form a set. Joining a Space you are already in changes nothing, and a leave removes that user.
  • If a user joins a Space that currently has no participants, a new active period starts at that join. The total active time is the sum of all active periods, so time while a Space is empty does not count.
  • Every Space that has a create event appears in the output, even when its total is 0. Key order in the returned dictionary does not matter.

Constraints

  • 1 <= len(events) <= 10000
  • space_id and user_id are non-empty strings
  • event_type is one of "create" , "join" or "leave"
  • 0 <= int(timestamp) <= 10**9
  • Events may not be sorted by timestamp.
  • Each Space is created exactly once, and its create event is processed before any join or leave for that Space.
  • A leave event always names a user who is currently in that Space.

Examples

Input: events = [
  ["space1", "alice", "create", "0"],
  ["space1", "bob", "join", "5"],
  ["space1", "alice", "leave", "10"],
  ["space1", "bob", "leave", "15"]
]
Output: {"space1": 15}

Alice creates the Space at t = 0 and Bob joins at 5. Alice leaves at 10 while Bob is still present, and Bob's leave at 15 empties the Space, so the active time is 15 - 0 = 15.

Input: events = [
  ["spaceA", "user1", "create", "10"],
  ["spaceB", "user2", "create", "20"],
  ["spaceA", "user3", "join", "25"],
  ["spaceB", "user2", "leave", "30"],
  ["spaceA", "user1", "leave", "35"],
  ["spaceA", "user3", "leave", "40"]
]
Output: {"spaceA": 30, "spaceB": 10}

spaceA is active from its creation at 10 until user3 leaves at 40, which is 30. spaceB is active from 20 until user2 leaves at 30, which is 10.

Input: events = [
  ["s2", "u2", "create", "3"],
  ["s1", "u1", "leave", "4"],
  ["s1", "u1", "create", "0"],
  ["s1", "u3", "join", "6"],
  ["s1", "u3", "leave", "9"],
  ["s2", "u5", "join", "7"]
]
Output: {"s1": 7, "s2": 6}

In time order, s1 is active from 0 to 4, empty until u3 joins at 6, and active again until 9, for 4 + 3 = 7. s2 still has u2 and u5 at the end, so it counts up to the log's maximum timestamp 9, giving 9 - 3 = 6.