CodePark/ problems / API rate limiter
START PRACTICINGPRACTICE
Coding problem · Practical class design › Time windows and expiry

API rate limiter

mediumabout 30 minutes in an interview

Build a RateLimiter class. An API gateway uses it to limit how often each user can call the API.

new RateLimiter(limit, windowMs) sets the default rule. Each user may make at most limit allowed requests in any period of windowMs milliseconds.

Every call passes the current time as a timestamp in milliseconds. Timestamps never decrease from one call to the next, across all users. One run may have up to 100,000 calls.

Input: rl = new RateLimiter(2, 1000) [rl.allow("ana", 0), rl.allow("ana", 500), rl.allow("ana", 999), rl.allow("ana", 1000)] Output: [true, true, false, true]at time 1000, the request from time 0 is outside the window

It comes in 3 parts, each building on the last: Requests per window, Plans, Throttle report.