We use cookies for site analytics. Accept to help us understand how the site is used. See our Privacy Policy for details.
A medium design problem, graded against 6 test cases (3 of them hidden).
Build a working data structure to an API - LRU caches, rate limiters, and iterators.
Reach for it when you see: "Design a class supporting these operations in O(1)" - an API rather than a single function.
More Designproblems →Keep a queue of hit timestamps. Since hits arrive in non-decreasing order, the queue is naturally sorted. For `getHits(timestamp)`, pop from the front while the front timestamp is `<= timestamp - 300` (those are too old), then the queue length is the number of hits in the trailing 300-second window. Each timestamp is enqueued once and dequeued at most once.
The full reference solution in every supported language stays in the editor above - reveal it there once you have had a real attempt.
These apply to the pattern as a whole, not just this problem.