Problem 267243 · easy · Level 02 Linear Data Structures

Turnstile Clicks in the Last Few Minutes

queues · deque · class design

A stadium turnstile reports every click with an integer timestamp, and timestamps never go backwards from one call to the next. Security wants a rolling count of recent clicks. Write the class HitCounter:

  • HitCounter(window): the rolling count looks back over window consecutive timestamps.
  • hit(t): one click happened at time t.
  • count(t): return how many clicks have a timestamp from t - window + 1 up to t, both ends included.

Examples

Input:  ops  = ["HitCounter", "hit", "hit", "hit", "count", "count", "count", "hit", "count", "count"]
        args = [[10], [2], [5], [5], [8], [11], [12], [14], [14], [15]]
Output: [None, None, None, None, 3, 3, 2, None, 3, 1]
Explanation: count(11) covers times 2..11, so the hit at 2 still counts; count(12) covers 3..12 and drops it.
count(15) covers 6..15, where only the hit at 14 is left.

Constraints

  • 1 <= window <= 10**9
  • At most 10**4 calls in total
  • Target: amortised O(1) per call

Goals

  • Store timestamps in a deque and evict from the front
  • Keep each operation amortised O(1)
Starting Python…