KatInterview

Rolling Window Stats: Unique Senders and Peak Message Size

Data Structure DesignAuto-graded · 17 test cases
MediumAuto-gradedData Structure Designsliding-windowdequehash-map

Last updated

Replay a list of operations on a fixed-size sliding window, reporting the distinct sender count and the largest payload size after each insertion.

Rolling Window Stats

A service receives a stream of messages. Each message is a pair (sender_id, size). You must keep track of only the most recent K messages and, after every new message arrives, report two numbers about the messages currently in that window:

  1. how many different senders appear in it, and
  2. the largest size among its messages.

Operation replay

Instead of using a class, your function is given a list of operations and a parallel list of arguments, and it must replay them in order.

  • "init" with arguments [K]: starts a fresh, empty window of capacity K. Any earlier state is discarded completely. The output for this step is None.
  • "add_event" with arguments [sender_id, size]: appends the message to the window. If the window now holds more than K messages, the oldest one is dropped, so that at most K remain. The output for this step is the list [distinct_senders, max_size], computed on the window after this insertion and any removal.

Return a list with one entry per operation, in the same order as the input.

Details

  • A sender counts as present as long as at least one of its messages is still in the window. A sender who appears several times is counted once.
  • If fewer than K messages have been added, nothing is dropped.
  • Every add_event is guaranteed to come after at least one init. Several init operations may appear; each one fully resets the window.

Constraints

  • 1 <= K <= 200,000
  • The total number of add_event operations is at most 200,000.
  • sender_id is an integer.
  • size is a non-negative integer.
  • len(operations) == len(arguments).

Examples

Example 1

Input: rolling_window_stats(['init', 'add_event', 'add_event', 'add_event', 'add_event'], [[3], [1, 10], [2, 20], [1, 5], [3, 15]])

Output: [None, [1, 10], [2, 20], [2, 20], [3, 20]]

Example 2

Input: rolling_window_stats(['init', 'add_event', 'add_event', 'add_event', 'add_event', 'add_event'], [[2], [7, 4], [7, 9], [8, 1], [7, 2], [9, 9]])

Output: [None, [1, 4], [1, 9], [2, 9], [2, 2], [2, 9]]

Example 3

Input: rolling_window_stats(['init', 'add_event', 'add_event'], [[1], [1, 0], [2, 0]])

Output: [None, [1, 0], [1, 0]]

Hints

Hint 1

Treat the two questions separately: one structure for the number of distinct senders, another for the maximum size.

Hint 2

A hash map from sender to its occurrence count, updated on both insertion and eviction, gives the distinct count as the number of keys (remove a key when its count reaches zero).

Hint 3

For the maximum, avoid deleting from the middle: either keep a heap with arrival indices and discard stale tops lazily, or keep a deque of indices whose sizes are in non-increasing order.