Problem LeetCode 362. Design Hit Counter ↗ · NeetCode ↗· The solutions on this page are my own.

Hit Counter Visualizer

Two counters receive the same hits. To keep it watchable the window here is WINDOW = 12 s; the problem asks for 300 s, and the code is exactly the same.

The problem, step by step

Skip to the visualizer ↓

Count the hits of the last 5 minutes

A page records a hit every time someone visits it. At any moment you must be able to say how many hits came in during the last 300 seconds.

  1. What you get

    Two kinds of calls: hit(timestamp) records one hit, and getHits(timestamp) asks for the count. Timestamps are in seconds and never go backwards. Several hits can share a second.

  2. What you return

    getHits(t) returns how many hits have a timestamp after t − 300 and at most t. hit returns nothing.

  3. The rules

    • A hit that is exactly 300 seconds old no longer counts. A hit 299 seconds old still does.
    • Every hit in the same second counts, not just one per second.
    • getHits only reads. It records nothing.
  4. Try it by hand

    Calls
    hit(10), hit(10), hit(150), getHits(200), hit(305), getHits(309), getHits(310)
    Output
    3, 4, 2 for the three getHits calls
    1. hit(10), hit(10), hit(150): three hits recorded, two of them in the same second.
    2. getHits(200): the window reaches back 300 seconds, past the first hit, so all three count. The answer is 3.
    3. hit(305): a fourth hit.
    4. getHits(309): the window covers timestamps after 9, up to 309. The hits at 10, 10, 150 and 305 are all inside: 4.
    5. getHits(310): the window now covers timestamps after 10. The two hits at 10 are exactly 300 seconds old and drop out: 2.

    The visualizer below shrinks the window to 12 seconds so you can watch hits expire. The code is the same.

  5. Edge cases to check

    • The boundary: at t, a hit from t − 300 is out and a hit from t − 299 is in.
    • A burst of hits in one second: storing every hit costs memory for each one.
    • A long quiet period: old hits must still be cleared out, or they keep using memory.
    • The usual follow up: what if there are millions of hits per second?
  6. The question behind the solutions

    Keeping every timestamp and dropping old ones from the front is simple and exact, but its memory grows with traffic. Can you answer with a fixed amount of memory, however busy the page gets?

Both counters

The calls from above with the real 300 second window, sketched the way you would draw it in an interview: hit(10), hit(10), hit(150), hit(305), then getHits(309) and getHits(310). Then watch both run below, with the window shrunk to 12 seconds.

1. Queue of timestamps

Remembers: the timestamp of every hit in the window, oldest at the front.

after hit 10, 10, 150 and 305:1010150305frontbackgetHits(309): drop while front ≤ 91010150305size 4 ✓getHits(310): drop while front ≤ 101010150305size 2 ✓one entry per hit: memory grows with traffic
  1. hit(t): add t to the back.
  2. getHits(t): drop from the front while the front is ≤ t − 300.
  3. Return the size of the queue.

The catch: exact, but a million hits a second means a million entries a second, and old hits stay in memory until the next getHits clears them.

2. Circle of 300 buckets

Remembers: two arrays of 300: the second each slot was last used for, and its hit count.

300slotsslot = t % 300slottimehitsat 31053051in10102out: 300 s1501501incount a slot when310 − time < 300getHits(310) = 1 + 1 = 2 ✓hit(310) lands on slot 10: its time is old,so reset the count to 0, then add 1
  1. hit(t): slot i = t % 300. If times[i] is not t, the slot is stale: set it to t and its count to 0. Add 1.
  2. getHits(t): add up the counts of every slot with t − times[i] < 300.

The catch: fixed memory however busy the page is, but it only knows whole seconds, and every getHits reads all 300 slots.

Two ways to solve it, step by step

Traffic
Speed
timestamp0

Hits per secondshaded = the window getHits(now) counts: (now − 12, now]

inside the window outside the window but still stored window bucket for the current second

Queue of timestamps

hit O(1)getHits O(1) amortizedspace O(hits in window), grows with traffic

Circle of 12 buckets

24 ints, always

hit O(1)getHits O(WINDOW) = O(1)space O(WINDOW), fixed

Last getHits call

–queue
–buckets

Complexity

Algorithmhit()getHits()SpacePrecisionBest for
Queue of timestampsO(1)O(1) amortizedO(hits in window)Exact timestampsLow traffic
Circle of bucketsO(1)O(WINDOW)O(WINDOW)1-second bucketsHigh traffic, the usual follow-up answer

Versus a rate limiter: a hit counter only records and reports; it never rejects anything. Add if (getHits(now) < limit) hit(now) as one locked step and the queue version becomes SlidingWindowLogRateLimiter.

Related in the Lab Rate Limiter, a counter that also rejects · Logger Rate Limiter

© 2026 Arman Ajabkhani · armantik.com · Privacy