Design a Rate-Based Alerting System for API Requests
Problem: Design an alerting solution for an API. Given a (sorted) stream/array of request timestamps (in seconds), send a notification after a request if either:
- there were more than 3 requests in the last 1 second, or
- there were more than 25 requests in the last 10 seconds.
Compute how many notifications need to be sent out in total.
Example: [1,1,1,1,1,2,2,2,3,3,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,15] -> 4 notifications.
Approach: A naive solution uses a single sliding-window queue tracking counts, but doesn't scale cleanly to production volumes. A more scalable design uses two independent sliding windows (one 1-second, one 10-second) implemented as separate counters/queues (or bucketed counters keyed by second) so each condition is checked independently in O(1) amortized per incoming request, avoiding a single queue holding all raw timestamps. At high request volume, bucketed/rolling counters (rather than storing every raw timestamp) keep memory bounded.