Implement a sliding-window rate limiter
RedditSoftware Engineer · OnsiteLast updated
This problem tests your ability to design a rate-limiting algorithm that calculates traffic volume within a sliding time window to prevent API abuse.
Sliding Window Rate Limiter
Design a rate limiter for a distributed system that restricts the number of requests a user can make within a rolling time window. Implement a class RateLimiter that processes incoming requests for a specific user_id.
Requirements
- The rate limiter must support a limit of
max_requestsperwindow_size(in seconds). - The
allow_request(user_id)method should returnTrueif the request is permitted, andFalseotherwise. - You must account for the sliding nature of the window (i.e., if the window is 60 seconds, a request at time
Tshould be compared against the count of requests in[T - 60, T]).
Function Signature
class RateLimiter:
def __init__(self, max_requests: int, window_size: int):
# max_requests: max allowed requests per window
# window_size: time window in seconds
pass
def allow_request(self, user_id: str) -> bool:
# Returns True if request is allowed, else False
pass
Examples
Example 1:
max_requests = 2,window_size = 5allow_request("user1")->True(T=1)allow_request("user1")->True(T=2)allow_request("user1")->False(T=3, limit reached)allow_request("user1")->True(T=7, window is now [2, 7])
Constraints
- Time is represented by epoch timestamps in seconds.
- Assume a single-threaded environment for the implementation.
- Optimize for time complexity where possible.
Hints
Hint 1
Consider storing timestamps for each user in a data structure that allows easy removal of expired entries.
Hint 2
A Queue or a Deque is well-suited for tracking timestamps since requests arrive in chronological order.
Hint 3
For every new request, remove all timestamps from the user's history that fall outside the current window before checking the size of the remaining collection.
Follow-up questions
- How would you modify this implementation to work in a distributed system with multiple application nodes?
- If you have millions of users, how would you optimize memory usage to prevent the map from growing indefinitely?
- How does the sliding window log approach differ from a fixed window counter in terms of accuracy and resource consumption?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.