← Questions

Total listener time per live audio room from an event log

0
SpaceXAICodingLast asked Feb 2026

A coding interview question asked in SpaceXAI's onsite applied coding round, last asked Feb 2026. It tests intervals, hash map and event processing.

A live audio product logs events in time order: a room is created, users join and leave rooms, and a room ends. Compute, for every room, the total time users spent in it: the sum, over every stay, of the time between a user joining and that user leaving. A user still in a room when it ends is counted until the end. A user still in a room that never ends is counted until the end of the log.

Interface

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

  • room_listener_time(events: list[tuple[int, str, str, str | None]], log_end: int) -> dict[str, int]
  • Each event is (timestamp, kind, room_id, user_id) with kind one of "create", "join", "leave", "end"; user_id is None for create and end.
  • Every created room appears in the result, with 0 if nobody joined.
  • A join to a room that has ended is ignored. A second join by a user already in the room is ignored. A leave by a user who is not in the room is ignored.

Example

(0,  create, s1)
(2,  join,   s1, ann)
(5,  join,   s1, bo)
(9,  leave,  s1, ann)          # ann: 2 .. 9  = 7
(10, create, s2)
(12, join,   s2, ann)
(15, end,    s1)               # bo:  5 .. 15 = 10
(16, join,   s1, cy)           # s1 has ended, ignored
(18, leave,  s2, ann)          # ann: 12 .. 18 = 6
(20, join,   s2, ann)          # ann rejoins, still there at log_end
log_end = 25                   # ann: 20 .. 25 = 5

->  {"s1": 17, "s2": 11}

Constraints

  • Up to 10^6 events, timestamps non-decreasing integers.
  • A user may be in several rooms at once and may join the same room many times.

Clarifying questions

  • Does the host count as a listener, and is there a host event?
  • Can events arrive out of order or be duplicated?
  • Is a user allowed in two rooms at the same time?
  • For users who never leave, should time stop at the room's end, the last event, or a given "now"?
  • Should the answer be total user-time, or wall-clock time with at least one listener?

What a strong answer covers

  • One pass with a map from (room, user) to join time and a per-room set of current users, so end closes only that room's open stays.
  • O(n) time and memory proportional to users currently in rooms.
  • Every edge case named: duplicate joins, leaves without a join, joins after end, rejoins, rooms nobody joined, open stays at the end.
  • Tests built from small hand-checked logs like the one above.

Follow-ups

  • Total listening time per user instead of per room, or the top-k rooms by total time.
  • The peak number of simultaneous listeners in each room and when it happened.
  • Split the totals into hourly buckets, with stays that cross bucket boundaries.
  • The log is a live stream too large for memory: what state do you keep, and how do you handle late events?

Where it's asked

What it tests

intervals · hash map · event processing

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