Your question is API Rate Limiting. 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.
Amperos Health uses an API for patient-record access. Implement an exact sliding-window rate limiter that enforces both a per-client limit and a global per-endpoint limit.
Each request is represented as [timestamp, client_id, endpoint]. Requests may be provided out of chronological order, but requests with equal timestamps must be processed in their original input order. A request is allowed only if accepting it would keep both limits within their windows. Rejected requests do not count toward either limit. Return decisions in the original input order.
Implement rate_limit(requests, per_client_limit, global_limit, window), where requests is a list of requests, timestamps and limits are integers, and client IDs and endpoints are strings. Return a Boolean list with one value per request. For a request at time t, only previously accepted requests with timestamps strictly greater than t - window count. The current request counts immediately if it is accepted.
def rate_limit(requests, per_client_limit, global_limit, window):