KatInterview

Room Assignment and Leaderboard for Streaming Sessions

Data Structure DesignAuto-graded · 19 test cases
MediumAuto-gradedData Structure Designheapdesignsimulation

Last updated

Simulate a stream of operations that assigns sessions to the lowest-numbered free room and reports the top-k streams by total duration, tested with heaps and lazy deletion.

Room Assignment and Leaderboard

A platform runs live streaming sessions in virtual rooms and keeps a leaderboard of the streams that have been on air the longest. You are given a log of operations to replay, in order, and you must return the result of each one.

Operations

Two parallel lists describe the log. ops[i] is the name of the i-th operation and op_args[i] holds its arguments.

"schedule" with arguments [start, end, stream_id]

  • A session occupies the half-open interval [start, end).
  • Place it in the free room with the smallest room ID. Rooms are numbered from 0.
  • A room is free at time start if the session it last hosted ended at or before start. A session ending at 5 therefore frees its room for a session starting at 5.
  • If no existing room is free, open a new room. Its ID equals the number of rooms created so far.
  • The result of this operation is the room ID used.
  • The session adds end - start to the running total for stream_id.

"top_k_streams" with arguments [k]

  • Rank every stream seen so far by its total accumulated duration, largest first.
  • Streams with equal totals are ordered alphabetically by stream_id.
  • The result is the list of the first k stream IDs in that order.
  • If fewer than k distinct streams exist, return all of them in ranked order. If there are none, return [].

Your task

Implement process_session_log(ops, op_args). It returns a list with one entry per operation, in the same order. Each entry is an int for "schedule" or a list[str] for "top_k_streams".

Constraints

  • len(ops) == len(op_args) and is at most 10^5.
  • For "schedule": 0 <= start < end <= 10^9.
  • The start values of successive "schedule" operations are non-decreasing.
  • stream_id has length 1 to 32 and uses lowercase English letters (underscores may also appear).
  • For "top_k_streams": 1 <= k <= 1000.
  • Every operation name is either "schedule" or "top_k_streams".

Examples

Example 1

Input: process_session_log(['schedule', 'schedule', 'schedule', 'schedule', 'top_k_streams'], [[1, 5, 'stream_a'], [2, 6, 'stream_b'], [5, 8, 'stream_c'], [7, 10, 'stream_a'], [2]])

Output: [0, 1, 0, 1, ['stream_a', 'stream_b']]

Example 2

Input: process_session_log(['schedule', 'schedule', 'top_k_streams', 'schedule', 'top_k_streams'], [[0, 10, 'z'], [0, 10, 'a'], [3], [10, 12, 'a'], [2]])

Output: [0, 1, ['a', 'z'], 0, ['a', 'z']]

Hints

Hint 1

Treat room assignment and the leaderboard as two independent problems that share one input log.

Hint 2

For rooms, keep one min-heap of (end_time, room_id) for busy rooms and another min-heap of idle room IDs. Before each placement, move every room with end_time <= start into the idle heap.

Hint 3

For the leaderboard, totals only grow. Push (-total, stream_id) after every update and skip entries whose total no longer matches the current one when answering a query. Push the valid entries back afterwards.