Rate Limiter with a Sliding Window per Client
Updated
Problem statement
Given a limit, a window length windowMs in milliseconds and a list of requests from many clients, decide for each request whether a rate limiter allows or denies it, and return the decisions in input order.
Request i is sent by clientIds[i], and timestamps[i] is its time in milliseconds. The requests are listed in the order they reach the limiter: the times never go down, and requests with the same time are handled in input order.
Each allowed request counts against its client for windowMs milliseconds. An allowed request at time p counts at every time from p up to, but not including, p + windowMs; at time p + windowMs it no longer counts. When client c sends a request at time t, it is allowed if fewer than limit of c's earlier allowed requests still count at that moment. Otherwise it is denied. A denied request is not recorded, so it never counts. Every client is counted on its own, with the same limit and windowMs for all clients.
The limiter is also meant to stay correct when many threads call it at the same time, and to save memory by keeping no time that can no longer count. The tests run the requests one at a time from one thread and check only the decisions; they do not test locking or measure memory. The editorial explains how to handle both.
Function
checkRequests(limit: int, windowMs: int, clientIds: String[], timestamps: int[]) → boolean[]Examples
Example 1
limit = 2windowMs = 1000clientIds = ["api7","api7","web","api7","api7","web"]timestamps = [0,400,500,900,1000,1600]return = [true,true,true,false,true,true]api7 is allowed at 0 and 400. At 900, both of those are less than 1000 ms old, so the limit of 2 is reached and the request is denied. At 1000, the request from time 0 is exactly 1000 ms old and no longer counts, and the denied request at 900 never counted, so only 400 is left and the request is allowed. web has its own count, so both of its requests are allowed.
Example 2
limit = 1windowMs = 10clientIds = ["a","a","a","a"]timestamps = [5,5,14,15]return = [true,false,false,true]The second request at time 5 arrives at the same time as the first one, which counts, so it is denied. At 14 the request from 5 is only 9 ms old, so this one is denied too. At 15 it is exactly 10 ms old, so it no longer counts and the request is allowed.
Example 3
limit = 3windowMs = 100clientIds = ["x","y","x","x","x","y","x","x","x"]timestamps = [0,10,20,30,90,95,100,120,125]return = [true,true,true,true,false,true,true,true,false]x uses its 3 requests at 0, 20 and 30, so the one at 90 is denied. At 100 the request from 0 has left the window and the request is allowed. At 120 the request from 20 leaves, so 30 and 100 are counted and the request is allowed. At 125, 30, 100 and 120 are all in the window, so it is denied. y is never near its limit.
Constraints
n == clientIds.length == timestamps.length1 <= n <= 10001 <= limit <= 10001 <= windowMs <= 10^90 <= timestamps[i] <= 10^9timestamps[i - 1] <= timestamps[i]for everyi > 0- Each
clientIds[i]has1to10characters, each a lowercase letter or a digit.
More StackAdapt interview questions
- Subtract Two Large Number StringsPHONE SCREEN · Seen Nov 2026
- Running Medians and PercentilesPHONE SCREEN · Seen Aug 2026
- Nested Recipe Inventory ManagerPHONE SCREEN · Seen Jul 2026
- Highest Bid Campaign MatcherONSITE INTERVIEW · Seen Nov 2026
- Build a Tree from Preorder and Inorder TraversalsONSITE INTERVIEW · Seen Sep 2026
- Maximum Bananas Delivered by a CamelONSITE INTERVIEW · Seen Sep 2026
- Reservoir Sampling with Recorded DrawsONSITE INTERVIEW · Seen Sep 2026
Similar questions at other companies
- Per-Client API Rate LimiterDocker · PHONE SCREEN
- Per-Client Sliding-Window Rate LimiterCVS Health · PHONE SCREEN
- Sliding-Window Rate LimiterUber · PHONE SCREENUber interview questions
- Recent Advertisement Click CountsAmazon · PHONE SCREEN
- Delayed Streaming Duplicate SuppressionGoogle · PHONE SCREENGoogle interview questions
- Completions API Rate LimiterCursor · PHONE SCREENCursor interview questions
- Sliding-Window Rate LimiterStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
- Key-Value Store GET and PUT QPSDatabricks · PHONE SCREENDatabricks interview questions
- Rate Limiter with Global and Per-User LimitsOpenAI · ONSITE INTERVIEWOpenAI interview questions
- Per-Client Sliding-Window Rate LimiterGeneral Motors · OA