Limited time: AI code review, hints, mock interviews, whiteboard analysis, and all Pro features are unlocked. Enroll

Design Hit Counter

Medium

Topics
QueueDesignBinary SearchData Stream

Design a hit counter that counts hits received in the past 5 minutes (300 seconds). Implement HitCounter(), hit(timestamp) which records a hit at the given timestamp (in seconds), and getHits(timestamp) which returns the number of hits in the past 300 seconds — that is, hits with a recorded time in the range (timestamp - 300, timestamp]. Calls are made in non-decreasing time order.

Example 1

Input:  operations = ["HitCounter","hit","hit","hit","getHits","hit","getHits","getHits"], values = [[],[1],[2],[3],[4],[300],[300],[301]]
Output: [null,null,null,null,3,null,4,3]

Constraints

  • 1 <= timestamp <= 2*10^9
  • All calls are made with non-decreasing timestamps.
  • At most 300 calls per getHits window in the basic version.
Run ⌘' · Submit ⌘⏎
SensAI SensAI
Beta
Listening...
Tap mic to stop voice mode

Unlock AI Features

One click to activate - no payment, no credit card. Just sign in and you're in.

AI code review and hints
SensAI chat assistant
AI mock interviews
Whiteboard analysis
100% free during early access