Your question is Implement Rate Limiter Algorithm. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
Datadog API ingestion endpoints must limit each client to a fixed number of requests during a rolling time window. Given requests in nondecreasing timestamp order, determine whether each request should be accepted.
Implement a per-client sliding-window rate limiter. A request is accepted when that client has made fewer than limit accepted requests in the interval (timestamp - window, timestamp]. Rejected requests do not count toward future limits.
Implement rate_limit(requests, limit, window), where:
requests is a list of two-element lists [client_id, timestamp].client_id is a string.timestamp is an integer representing seconds.limit and window are positive integers.True means accepted, and False means rejected.Use an efficient data structure so processing each request does not require scanning all previous requests.
def rate_limit(requests, limit, window):