KatInterview

Sliding-Window Event Counter with a Circular Bucket Buffer

Data Structure DesignAuto-graded · 23 test cases
HardAuto-gradedData Structure Designring-buffersliding-windowdesign

Last updated

Design a fixed-memory event counter using a ring buffer of per-second buckets that tracks how many events fell inside a trailing 15-minute window.

Sliding-Window Event Counter with a Circular Bucket Buffer

You are building a lightweight monitoring component that tracks recent activity. The component must support two operations:

  • record(ts) — log one event that occurred at second ts.
  • count(now) — return the total number of events whose timestamps satisfy now - 900 < ts <= now (i.e., the most recent 15 minutes, including now but excluding the second exactly 900 seconds before it).

Memory constraint

Event volume can be arbitrarily high. The implementation must use O(1) memory — specifically, a circular array of 900 integer buckets (one per second in the window). No unbounded lists, queues, or maps are allowed.

Design notes

  • Timestamps are non-negative integers representing whole seconds.
  • Calls arrive with non-decreasing timestamps; you will not be asked to handle out-of-order events.
  • count always receives a timestamp that is ≥ every previously seen timestamp.
  • A bucket is considered stale when the timestamp it was last written for no longer falls inside the current 15-minute window. Stale buckets must be zeroed out before they are reused.
  • A gap between consecutive calls can exceed 900 seconds; the implementation must handle this correctly.

Operations

Your function receives two parallel lists:

parametermeaning
opslist of strings, each "record" or "count"
argslist of single-element lists, each containing the integer timestamp for that call

Return a list of the same length. Each entry is None for a "record" call, or an integer for a "count" call.

Constraints

  • 1 <= len(ops) <= 10^4
  • len(ops) == len(args)
  • Every element of args is a list containing exactly one non-negative integer.
  • Timestamps are non-decreasing across all calls.
  • Each timestamp satisfies 0 <= ts <= 10^9.
  • ops[i] is either "record" or "count".

Examples

Example 1

Input: sliding_window_counter(['record', 'record', 'count', 'record', 'count', 'count'], [[1], [10], [10], [20], [20], [920]])

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

Example 2

Input: sliding_window_counter(['record', 'record', 'record', 'count', 'count'], [[100], [100], [100], [100], [1000]])

Output: [None, None, None, 3, 0]

Example 3

Input: sliding_window_counter(['count', 'record', 'count'], [[0], [0], [0]])

Output: [0, None, 1]

Hints

Hint 1

A circular buffer of exactly 900 slots is enough because there are only 900 distinct seconds in a 15-minute window — map each timestamp to a slot with timestamp % 900.

Hint 2

A slot can hold data from an earlier cycle. Store the actual timestamp that last wrote each slot alongside its count; if that stored timestamp doesn't match the second you're asking about, treat the slot as empty (count zero).

Hint 3

For a count query at time now, iterate over all 900 candidate seconds in the range (now-899 .. now), check each slot's stored timestamp, and sum only the non-stale counts. This is O(900) = O(1) per query.