←ENG-DSA-065Real-time API Analytics (Queue/Deque)
00:00
🐍 Python Idle
Mode:Web

Real-time API Analytics (Queue/Deque)

CompaniesStripe, Datadog
Est. Time~25 min
LevelMid
DesignQueueStreaming
🚨 P0 Incident
StripeDatadog

API Gateways like Stripe must display "Requests in the last 5 minutes" on their analytics dashboards. Storing every timestamp forever causes Out-Of-Memory crashes. Using a sliding queue dynamically prunes data older than 300 seconds, maintaining a strict upper memory bound.

πŸ“₯ Assigned to:You β€” Mid Engineer
Your Task

Design a HitCounter class that tracks the number of API hits. It should support `hit(timestamp)` to record a hit, and `get_hits(timestamp)` to return the total hits in the past 5 minutes (300 seconds). Timestamps are strictly increasing.

⏱ Time: O(1) amortizedπŸ’Ύ Space: O(W) where W is max hits in 300 seconds
Example 1
Input:Β Β hit(1), hit(2), get_hits(4)
Output:Β 2
Example 2
Input:Β Β hit(1), get_hits(300), get_hits(301)
Output:Β 1 at 300, 0 at 301
πŸ”’ +3 hidden test cases β€” revealed on Submit
Hints
Loading editor…
Test Output
β–Ά Run Code to test against examples Β· Submit to judge all 5 test cases
EngPrep β€” Real Engineering. Real Interviews.