Build a simple rate limiter in Python that restricts how many requests a user can make per minute. You have approximately 15 minutes.
Implement a class with the following method:
is_allowed(user_id) -> bool- Returns
Trueif the request is allowed - Returns
Falseif the user has exceeded their rate limit - Each user is allowed N requests per minute (you can make N configurable, default to 10)
limiter = RateLimiter(max_requests=5) # 5 requests per minute per user
limiter.is_allowed("user-123") # True (1st request)
limiter.is_allowed("user-123") # True (2nd request)
limiter.is_allowed("user-123") # True (3rd request)
limiter.is_allowed("user-123") # True (4th request)
limiter.is_allowed("user-123") # True (5th request)
limiter.is_allowed("user-123") # False (exceeded limit)
limiter.is_allowed("user-456") # True (different user, separate limit)
# After 1 minute passes...
limiter.is_allowed("user-123") # True (limit resets)- Focus on getting a working solution first
- Think aloud as you work — we want to understand your thought process
- Feel free to ask clarifying questions
- You may use AI coding assistants if that's part of your normal workflow
- Consider: How do you track time? When does the "minute" window start/reset?