KatInterview

Ring-Buffer Event Counter with Time Windows

Data Structure DesignAuto-graded · 13 test cases
MediumAuto-gradedData Structure Designsliding-windowring-bufferdata-structure-design

Last updated

Tests whether a candidate can implement a fixed-memory sliding-window counter using a circular array of timestamped buckets with O(1) writes and O(W) reads.

Ring-Buffer Event Counter with Time Windows

You are building a lightweight event-tracking system. The system processes a sequence of named commands, each with a single integer argument representing a second-precision timestamp. Your job is to execute those commands in order and return the result of each one.

There are two commands:

  • "recordHit" with argument [t] — log one event at time t. This command produces no output (None).
  • "getHits" with argument [t] — report how many events have been recorded with timestamps strictly greater than 0 and at most t. This command returns an integer.

The system keeps events in a fixed-size circular array of window_size slots. Second t is stored in slot t % window_size. Each slot holds the timestamp of the last write and a count; if two different seconds map to the same slot, the newer one overwrites the older one.

Command rules

  • Timestamps across all commands (both recordHit and getHits) must be non-decreasing. A timestamp strictly less than the most recent timestamp seen so far raises ValueError.
  • An unrecognized command name raises ValueError.
  • If the operation list is empty, return an empty list.

Collision handling

Because the array has only window_size slots, second t and second t + window_size share the same slot. Each slot therefore stores the exact timestamp of its most recent write alongside the count:

  • If an incoming recordHit finds a slot whose stored timestamp equals t, it increments that slot's count (same second, same event bucket).
  • If the stored timestamp differs from t, the slot holds stale data from an earlier pass around the ring. Overwrite it with timestamp t and count 1.

What getHits counts

When called with argument t, getHits scans every slot and sums the counts of all slots whose stored timestamp ts satisfies 0 < ts <= t. Because t is both the current time and the look-back length under this command contract, the lower bound is always 0 — events recorded at second 0 are never included.

Constraints

  • 1 <= window_size <= 10_000
  • 0 <= len(operations) <= 10_000
  • len(operations) == len(arguments)
  • Each element of arguments is a list containing exactly one integer.
  • 0 <= timestamp <= 10^9
  • Timestamps across all operations are non-decreasing; any violation raises ValueError.
  • An unknown operation name raises ValueError.
  • Multiple recordHit calls at the same timestamp are allowed and are counted together.

Examples

Example 1

Input: ring_buffer_event_counter(['recordHit', 'recordHit', 'getHits', 'recordHit', 'getHits'], [[1], [1], [1], [2], [2]], 300)

Output: [None, None, 2, None, 3]

Hints

Hint 1

Map each timestamp to a slot with t % window_size, but store the actual timestamp alongside the count so you can tell fresh data apart from stale data that happens to share the same slot index.

Hint 2

When a recordHit arrives at slot i: if the stored timestamp matches t, increment; if it differs (or the slot is empty), reset to [t, 1]. A mismatch always means the existing data is older, because timestamps never go backward.

Hint 3

For getHits, scan all window_size slots and sum counts where 0 < stored_timestamp <= t. The lower bound is 0 because t minus t equals 0 under this contract — hits at second 0 are excluded.