NewProject Coding is here — build features and fix bugs in real codebases with an AI assistantSee what's new

You are building a rate limiter for a backend system. Incoming requests arrive at times specified by the array timestamps. Your task is to determine, for each request, whether it should be allowed or rejected based on the following rule:

  • Each request at time t is allowed if and only if, after including it, there are at most maxRequests requests in the time window [t - windowSize + 1, t] (inclusive).
  • Only requests that are allowed count toward the limit for future windows. Rejected requests do not contribute to capacity.

Return an array result where result[i] is set to true if the ithi^{\text{th}} request is allowed, and false if it is rejected.

Constraints:

  • 1 ≤ timestamps.length ≤ 10510^5
  • 1 ≤ timestamps[i] ≤ 10910^9
  • 1 ≤ maxRequests ≤ 10510^5
  • 1 ≤ windowSize ≤ 10910^9
  • timestamps is strictly increasing.

Example 1:

Input: timestamps = [1, 100, 200, 250, 350], maxRequests = 2, windowSize = 200 Output: [true, true, false, true, true] Explanation:

  • At time 1, only 1 request so far in window [1, 1] => allow.
  • At time 100, two requests in [1, 100], both allowed so far => allow.
  • At time 200, three requests in [1, 200], but limit is 2 => reject.
  • At time 250, the window is [51, 250], two allowed requests in this window (100, 250) => allow.
  • At time 350, window is [151, 350], two allowed requests (250, 350) => allow.

Example 2:

Input: timestamps = [10, 20, 30, 40], maxRequests = 3, windowSize = 50 Output: [true, true, true, false]

Example 3:

Input: timestamps = [1, 2, 3, 10, 11, 12, 20, 21, 22, 23], maxRequests = 2, windowSize = 5 Output: [true, true, false, true, true, false, true, true, false, false]


Accepted
3.9k/7.4k
Acceptance Rate53.0%
Test Cases
Input
timestamps =
maxRequests =
windowSize =
Output
Expected