FastPrepRate Limiter with a Sliding Window per Client
Problems/Rate Limiter with a Sliding Window per Client

Rate Limiter with a Sliding Window per Client

StackAdapt logoStackAdapt● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported as the phone screen of a StackAdapt interview for a full-time software engineering role. The interviewer gave the task as a short written list of requirements: limit each client on its own over a time window that moves with the clock; for each request, say yes or no and keep a record of it only on yes; stay correct when many threads send requests at once; and use little memory by throwing away times that have run out. The interviewer then asked a few system design questions about the same limiter. Those are not part of this problem.

The report gives no interface, no examples and no limits, so these choices are ours. One function gets all the requests in order and returns one decision for each, instead of a class that is called once per request. Times are whole milliseconds. We assume the requests reach the limiter in time order, so the times never go down, and requests with the same time are handled in input order. An allowed request stops counting exactly windowMs milliseconds after its time, so a request at that moment no longer sees it. One limit and one window length apply to every client. The limits and all the examples are ours. The tests call the function from one thread and check only the decisions, so thread safety and memory use are described in the statement and the editorial but are not tested.

Our version is about 80% the same as the reported question.

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.length
  • 1 <= n <= 1000
  • 1 <= limit <= 1000
  • 1 <= windowMs <= 10^9
  • 0 <= timestamps[i] <= 10^9
  • timestamps[i - 1] <= timestamps[i] for every i > 0
  • Each clientIds[i] has 1 to 10 characters, 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
All StackAdapt interview questionsSee StackAdapt hiring insights
Saved
class Solution {
    public boolean[] checkRequests(int limit, int windowMs, String[] clientIds, int[] timestamps) {
        // Write your code here.
    }
}
Checking account…