KatInterview

Rolling Mean of Payload Lengths Over a 60-Second Event Window

Data Structure DesignAuto-graded · 15 test cases
MediumAuto-gradedData Structure Designdequesliding-windowhash-set

Last updated

Process a stream of checksummed events with duplicates and out-of-order timestamps, reporting a running average payload length over a 60-second window using a queue, a set and a running sum.

You are given a list of events that arrive one after another, possibly with timestamps out of order. After each event is handled, report the average payload length of the events currently counted in a 60-second window.

Each event is a dictionary with these keys:

  • id: an identifier (string)
  • timestamp: an integer number of seconds
  • payload: a string
  • checksum: a string

Write windowed_payload_mean(events), which returns a list with exactly one float per input event. Entry i is the average payload length right after event i has been processed.

Processing rules

Keep these pieces of state:

  • A clock, initially unset.
  • A window queue of accepted events, ordered by arrival (not by timestamp). Each entry stores its timestamp, id and payload length.

Handle each event in order:

  1. Checksum. The event is valid only if checksum is exactly equal to payload (string equality). An invalid event is ignored completely: the clock does not change and nothing is removed or added. The current average is still reported.

  2. Advance the clock. For a valid event, set clock = max(clock, timestamp). This happens for every valid event, even one that is later rejected as late or as a duplicate. If the clock was unset, it becomes this event's timestamp.

  3. Expire. Let cutoff = clock - 60. Look at the front (oldest arrival) of the window queue. While the front entry has a timestamp strictly less than cutoff, remove it. Stop at the first front entry that is not expired, even if entries behind it have older timestamps. Removed entries no longer count, and their ids are free to be accepted again.

  4. Admit. Append the current event to the back of the queue only if both hold:

    • timestamp >= cutoff (an event exactly 60 seconds behind the clock is still accepted), and
    • its id is not currently held by any entry in the window queue.

    Otherwise the event is discarded. When an id is accepted, the first copy wins, and a later copy with a different payload does not replace it.

  5. Report. Append to the result the sum of payload lengths in the queue divided by the number of entries in the queue, or 0.0 if the queue is empty.

An empty payload with an empty checksum is valid and has length 0.

Constraints

  • 0 <= len(events) <= 10^5
  • Timestamps are integers and may be negative or repeated.
  • Every event contains all four keys.
  • The result must be computed in at most O(n log n) time overall.
  • Return an empty list for empty input.

Examples

Example 1

Input: windowed_payload_mean([{'id': 'a', 'timestamp': 0, 'payload': 'hi', 'checksum': 'hi'}, {'id': 'b', 'timestamp': 30, 'payload': 'world', 'checksum': 'world'}, {'id': 'c', 'timestamp': 61, 'payload': 'x', 'checksum': 'x'}])

Output: [2, 3.5, 3]

Example 2

Input: windowed_payload_mean([{'id': 'a', 'timestamp': 10, 'payload': 'ok', 'checksum': 'bad'}, {'id': 'b', 'timestamp': 20, 'payload': 'yes', 'checksum': 'yes'}])

Output: [0, 3]

Hints

Hint 1

Keep a running total of payload lengths and a count of queued entries so each average is O(1) to compute instead of rescanning the window.

Hint 2

Use a deque ordered by arrival plus a set of ids currently in the deque. Whenever you pop an entry from the front, also remove its id from the set and subtract its length from the total.

Hint 3

Expiry only inspects the front of the deque and stops at the first non-expired entry, even if later entries are older. Invalid events must skip the clock update, but still output the current average.