Design Hit Counter
Medium · Queue & Deque · about 15 minutes
Asked at Databricks, Apple, Roblox, Uber, Bloomberg, Google, Dropbox, Two Sigma, Notion, Datadog, Elastic, Riot Games, Unity, Cohere, Hugging Face, Airtable, Rivian, Rubrik.
Implement HitCounter, which counts hits over the most recent 5 minutes (300 seconds). Timestamps are in seconds and never decrease from one call to the next; several hits can share a timestamp.
HitCounter()starts with no hits.hit(timestamp)records one hit attimestamp.getHits(timestamp)returns how many hits fall in the window(timestamp - 300, timestamp].