Loading...
Loading...
1 <= maxRequests <= 1000 1 <= windowSizeInSeconds <= 3600 1 <= number of requests <= 10^4 Timestamps are non-decreasing integers: 0 <= timestamp <= 10^6 clientId is a non-empty alphanumeric string, length <= 50 Number of unique clients <= 500
You are building a public-facing hotel search API and need to implement a Token Bucket Rate Limiter to protect the backend from abuse. Each unique API client (identified by their clientId) is allowed a fixed number of requests per time window.
Your task is to implement the core logic of a RateLimiter class that tracks request counts per client and decides whether each incoming request should be allowed or rejected.
Implement a RateLimiter class with the following behavior:
maxRequests tokens.windowSizeInSeconds seconds (fixed window approach).clientId, check if they have remaining tokens in the current window.true (request allowed).false (request rejected).You are given a list of operations to simulate. Each operation is a tuple:
[clientId, timestamp]
clientId — string identifier for the API clienttimestamp — integer representing seconds since epochGiven maxRequests (int) and windowSizeInSeconds (int), process each request in order and return a list of booleans indicating whether each request was allowed.
Example 1: maxRequests=3, windowSizeInSeconds=10
Requests: [("client1", 1), ("client1", 2), ("client1", 3), ("client1", 4), ("client1", 11)]
Output: [true, true, true, false, true]
Explanation: client1 uses all 3 tokens in window [1,10]. The 4th request at t=4 is rejected. At t=11 a new window starts, so it's allowed.
Example 2: maxRequests=2, windowSizeInSeconds=5
Requests: [("a", 1), ("b", 1), ("a", 2), ("b", 3), ("a", 3), ("b", 6)]
Output: [true, true, true, true, false, true]
Explanation: Clients a and b have independent buckets. a exhausts tokens at t=3; b gets a fresh window at t=6.
{clientId -> [requestCount, windowStartTime]}.windowId = timestamp / windowSizeInSeconds.windowStartTime differs from current windowId, reset the count.maxRequests and increment or reject.ConcurrentHashMap).public List<Boolean> processRequests(int maxRequests, int windowSizeInSeconds, List<int[]> requests)
// requests[i] = [clientIdIndex, timestamp], clientIds provided separately