Your question is Rate Limiter Class. 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.
Grammarly services need to prevent a client from sending too many requests in a short period. Implement a rate limiter that processes request timestamps in chronological order and determines whether each request is accepted.
A request at time t is accepted when fewer than limit accepted requests occurred in the half-open interval (t - 1, t] before it is evaluated. Rejected requests do not consume capacity. Exactly one second-old requests are outside the window and must be removed before evaluating the current request.
Expose the algorithm through simulate_rate_limiter(timestamps, limit), which models a stateful RateLimiter class and returns one Boolean decision per timestamp.
timestamps, a nondecreasing list of numbers representing request times in seconds, and positive integer limit.i is True if the request at timestamps[i] is accepted, otherwise False.def simulate_rate_limiter(timestamps, limit):