Sliding Window Rate Limiter

Algorithm Sliding Window Rate Limiter

Your task is to implement a rate limiter that operates on a sliding time window.

Problem Description:

You are given:

  • A list of request timestamps in ascending order (each timestamp is an integer representing seconds).
  • An integer maxRequests representing the maximum allowed requests in any continuous time window of windowSize seconds.
  • An integer windowSize representing the size of the sliding time window in seconds.

For each request in the list, determine whether it should be allowed. A request is allowed if and only if the number of requests (including the current one) that have occurred in the last windowSize seconds (i.e., from timestamp t - windowSize + 1 to timestamp t) does not exceed maxRequests.

Return a list (or array) of booleans indicating for each request if it is allowed (true) or rejected (false).

Example:

Consider the following input:

requestTimestamps = [1, 2, 3, 4, 5, 6]
maxRequests = 3
windowSize = 3
  • For timestamp 1, the window [ -1, 1 ] has 1 request → Allowed.
  • For timestamp 2, the window [ 0, 2 ] has 2 requests → Allowed.
  • For timestamp 3, the window [ 1, 3 ] has 3 requests → Allowed.
  • For timestamp 4, the window [ 2, 4 ] (the request at time 1 falls out) has 3 requests → Allowed.
  • For timestamp 5, the window [ 3, 5 ] has 3 requests → Allowed.
  • For timestamp 6, the window [ 4, 6 ] has 3 requests → Allowed.

If a request would cause the count to exceed maxRequests, it should be marked as rejected.

Your Task:

Write a function or method that processes the list of timestamps and returns the corresponding list of booleans (or similar) indicating if each request is allowed. Consider efficiency in your solution and do not use any external libraries that provide rate limiting functionality.


Loading...