Bird
Raised Fist0
HLDsystem_design~10 mins

Design a rate limiter in HLD - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define the main component responsible for limiting requests.

HLD
class [1]Limiter:
    def __init__(self, max_requests, window_seconds):
        self.max_requests = max_requests
        self.window_seconds = window_seconds
        self.requests = {}  # user_id -> list of timestamps
Drag options to blanks, or click blank then click option'
ARate
BRequest
CLimit
DThrottle
Attempts:
3 left
💡 Hint
Common Mistakes
Using generic names like 'RequestLimiter' which is less common.
Using 'Throttle' which is related but not the standard class name.
2fill in blank
medium

Complete the code to check if a user can make a request within the rate limit window.

HLD
def is_allowed(self, user_id, current_time):
    if user_id not in self.requests:
        self.requests[user_id] = []
    self.requests[user_id] = [t for t in self.requests[user_id] if t > current_time - [1]]
    return len(self.requests[user_id]) < self.max_requests
Drag options to blanks, or click blank then click option'
Aself.window_seconds
Bself.max_requests
Ccurrent_time
Duser_id
Attempts:
3 left
💡 Hint
Common Mistakes
Using max_requests instead of window_seconds for filtering timestamps.
Using current_time directly without subtracting the window.
3fill in blank
hard

Fix the error in the code that adds a new request timestamp after checking allowance.

HLD
def add_request(self, user_id, current_time):
    if self.is_allowed(user_id, current_time):
        self.requests[user_id].[1](current_time)
        return True
    return False
Drag options to blanks, or click blank then click option'
Aadd
Bappend
Cinsert
Dextend
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'add' which is for sets, not lists.
Using 'extend' which expects an iterable, not a single element.
4fill in blank
hard

Fill both blanks to implement a sliding window rate limiter that removes old requests and checks the count.

HLD
def is_allowed(self, user_id, current_time):
    self.requests[user_id] = [t for t in self.requests.get(user_id, []) if t [1] current_time - self.window_seconds]
    return len(self.requests[user_id]) [2] self.max_requests
Drag options to blanks, or click blank then click option'
A>
B<
C>=
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>=' instead of '>' which may keep one extra old timestamp.
Using '>=' or '>'' for the count comparison which would block one extra request.
5fill in blank
hard

Fill all three blanks to implement a dictionary comprehension that tracks request counts per user within the window.

HLD
request_counts = {user: len([t for t in times if t [1] current_time - window]) for user, times in [2].items() if len(times) [3] 0}
Drag options to blanks, or click blank then click option'
A>
Brequests
Dself.requests
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'requests' instead of 'self.requests' which is undefined in this scope.
Using '>=' instead of '>' which may include old timestamps.
Using 'len(times) >=' 0 which is always true and unnecessary.