Loading...
Loading...
You are building a distributed API gateway system where multiple server instances share traffic load. To prevent abuse and ensure fair usage, you need to implement a sliding window rate limiter that works consistently across all instances.
Each gateway instance records API requests with a timestamp. Given a unified log of requests across all instances, determine which requests should be allowed or denied based on a global rate limit.
The rate limit rule is: a user can make at most maxRequests requests within any sliding window of windowSize seconds.
You are given:
requests: a list of [userId, timestamp] pairs representing API requests arriving in chronological order (timestamps in seconds, possibly from multiple gateway instances but merged and sorted).maxRequests: the maximum number of requests allowed per user within the time window.windowSize: the duration of the sliding window in seconds.Return a list of strings — "allowed" or "denied" — for each request in the same order.
Example 1:
requests = [["user1", 1], ["user1", 2], ["user1", 3], ["user1", 4]]
maxRequests = 3
windowSize = 3
Output: ["allowed", "allowed", "allowed", "denied"]
Explanation: For user1:
Example 2:
requests = [["user1", 1], ["user2", 1], ["user1", 2], ["user2", 5]]
maxRequests = 2
windowSize = 3
Output: ["allowed", "allowed", "allowed", "allowed"]
Explanation: Each user is tracked independently. Both users stay within limit in their respective windows.
Example 3:
requests = [["user1", 1], ["user1", 1], ["user1", 1]]
maxRequests = 2
windowSize = 5
Output: ["allowed", "allowed", "denied"]
Explanation: All three requests come at the same timestamp. The first two are allowed (up to maxRequests=2). The third is denied.
1 <= requests.length <= 10^4 requests[i] = [userId, timestamp] 1 <= userId.length <= 20 1 <= timestamp <= 10^9 Timestamps are in non-decreasing order 1 <= maxRequests <= 100 1 <= windowSize <= 10^9
(currentTimestamp - windowSize, currentTimestamp].maxRequests, allow and record it; otherwise deny.