FastPrepCompletions API Rate Limiter
Problems/Completions API Rate Limiter

Completions API Rate Limiter

Cursor logoCursor● MediumFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: In this phone screen, the candidate built a rate limiter over an in-memory list of requests, where each request has a user, an endpoint, a body size in bytes, and a time in seconds. Part 1 limited v1/completions/standard to 3 requests per second for each user, and denied requests did not count. Part 2 turned this into a general interface that runs several limits together: 7 per minute on v1/completions/chunked, a cap that lets v1/completions/chunked requests through only while the total bytes in the last minute stay within 100, 5 per second on v2/completions/chunked, 7 per hour across all endpoints, and the Part 1 limit. A request counts only if every limit allows it.

The report does not say exactly where a time window starts and ends, which requests' bytes count toward the 100, whether that total includes the new request, whether the hourly limit is per user, how endpoint names are matched, or how requests with the same time are ordered. Here, windows slide with each request and a request exactly one window old no longer counts, only the user's accepted v1/completions/chunked bytes count toward the 100, the new request's bytes count too, every limit including the hourly one is per user and covers every endpoint, endpoint names must match exactly, and requests run in the order given. The size limits are practice choices. You get the log as four arrays and return true or false for each request, and both parts are judged together as one function.

The judged task matches the report at about 85%.

You are building a rate limiter for an API. You get a log of n requests. Request i was sent by user userIds[i] to the endpoint urls[i], has a body of bodySizes[i] bytes, and arrived at second timestamps[i].

Go through the requests one at a time, in the order given. Each request is either accepted or denied by the rules below. Return a boolean array answer where answer[i] is true if request i is accepted and false if it is denied.

Rules

Every rule is checked per user: only the same user's earlier accepted requests count.

RuleApplies toLimit
1v1/completions/standardat most 3 requests in the last 1 second
2v1/completions/chunkedat most 7 requests in the last 60 seconds
3v2/completions/chunkedat most 5 requests in the last 1 second
4v1/completions/chunkedat most 100 bytes of body in total in the last 60 seconds
5every endpointat most 7 requests in the last 3600 seconds

How the rules work

  • Rules 1 to 4 apply only when urls[i] is exactly the endpoint in the table. Rule 5 applies to every request, so any other endpoint, such as v1/models, is limited only by rule 5.
  • For a request at second t, the last W seconds means the times greater than t - W and up to t. An accepted request from exactly W seconds earlier no longer counts. For a 1-second rule, this means only requests from the same second count.
  • A count rule with a limit of L passes if, in its window, the user has fewer than L accepted requests that the rule applies to.
  • Rule 4 passes if the body sizes of the user's accepted v1/completions/chunked requests in the window, plus the new request's own body size, add up to at most 100.
  • A request is accepted only if every rule that applies to it passes. If even one rule fails, the request is denied, and it never counts toward any rule later, not even the rules it passed.
  • Requests with the same timestamp are handled in array order.

Function

processRequests(userIds: String[], urls: String[], bodySizes: int[], timestamps: int[]) → boolean[]

Examples

Example 1

userIds = ["a","a","a","a","b","a"]urls = ["v1/completions/standard","v1/completions/standard","v1/completions/standard","v1/completions/standard","v1/completions/standard","v1/completions/standard"]bodySizes = [5,5,5,5,5,5]timestamps = [10,10,10,10,10,11]return = [true,true,true,false,true,true]

All six requests go to v1/completions/standard, so rule 1 (3 per second) applies, and rule 5 applies as always.

  • User a sends three requests at second 10. All are accepted.
  • The fourth request from a at second 10 would make 4 in the same second, so it is denied.
  • User b has no requests yet. Limits are per user, so its request at second 10 is accepted.
  • At second 11, the last 1 second holds only times greater than 10, so user a has 0 requests there and is accepted again. Counting this one, user a has 4 accepted requests in the last hour, so rule 5 passes too.

Example 2

userIds = ["u1","u1","u1","u1","u1"]urls = ["v1/completions/chunked","v1/completions/chunked","v1/completions/chunked","v1/completions/chunked","v1/completions/chunked"]bodySizes = [60,50,40,1,30]timestamps = [0,20,30,59,60]return = [true,false,true,false,true]

Every request goes to v1/completions/chunked, so rule 4 (at most 100 bytes in the last 60 seconds) decides here. Rules 2 and 5 never come close.

  • Second 0: 60 bytes. Accepted, 60 bytes in the window.
  • Second 20: 60 + 50 = 110, which is over 100. Denied, and its 50 bytes do not count.
  • Second 30: 60 + 40 = 100, which is not over 100. Accepted.
  • Second 59: 100 + 1 = 101. Denied.
  • Second 60: the window holds times greater than 0, so the 60-byte request has left. Only the 40-byte request is still inside, and 40 + 30 = 70. Accepted.

Example 3

userIds = ["x","x","x","x","x","x","x","x","x","x"]urls = ["v2/completions/chunked","v2/completions/chunked","v2/completions/chunked","v2/completions/chunked","v2/completions/chunked","v2/completions/chunked","v1/models","v1/completions/standard","v1/models","v1/models"]bodySizes = [0,0,0,0,0,0,0,0,0,0]timestamps = [100,100,100,100,100,100,200,300,3699,3700]return = [true,true,true,true,true,false,true,true,false,true]
  • The first five v2/completions/chunked requests at second 100 are accepted. The sixth is denied by rule 3 (5 per second).
  • v1/models has no rule of its own, but rule 5 counts every endpoint. It is accepted at second 200, and the v1/completions/standard request at second 300 is accepted too. User x now has 7 accepted requests.
  • At second 3699, the last 3600 seconds are times greater than 99. All 7 accepted requests are inside, so it is denied by rule 5.
  • At second 3700, the window is times greater than 100. The five requests from second 100 have left, so only 2 remain, and the request is accepted.

Constraints

  • 1 <= userIds.length <= 1000
  • urls.length == bodySizes.length == timestamps.length == userIds.length
  • 1 <= userIds[i].length <= 10
  • userIds[i] contains only lowercase English letters and digits.
  • 1 <= urls[i].length <= 30
  • urls[i] contains only lowercase English letters, digits, and /.
  • 0 <= bodySizes[i] <= 1000
  • 0 <= timestamps[i] <= 10^9
  • timestamps is sorted in non-decreasing order.

More Cursor problems

  • Diff Two Filesystem Hash TreesPHONE SCREEN · Seen Sep 2026
  • Filesystem Hash TreePHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
All Cursor interview questionsSee Cursor hiring insights
Saved
public boolean[] processRequests(String[] userIds, String[] urls, int[] bodySizes, int[] timestamps) {
    // Write your solution here.
}
Checking account…