0
0
Rest APIprogramming~20 mins

Why rate limiting protects services in Rest API - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Rate Limiting Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
What is the main purpose of rate limiting in APIs?

Imagine a busy coffee shop that only has 10 seats. What is the main reason the shop owner would limit the number of customers inside at once?

Similarly, why do APIs use rate limiting?

ATo prevent too many requests from overwhelming the service and causing it to slow down or crash.
BTo make the API slower on purpose so users wait longer.
CTo allow unlimited requests from all users without any restrictions.
DTo block all users from accessing the API permanently.
Attempts:
2 left
💡 Hint

Think about how too many people in a small space can cause problems.

Predict Output
intermediate
1:30remaining
What will this rate limiting code output when requests exceed the limit?

Consider this simplified Python code snippet for rate limiting an API endpoint:

requests = 0
limit = 3

for i in range(5):
    if requests < limit:
        requests += 1
        print("Request allowed")
    else:
        print("Too many requests")

What is the output?

Rest API
requests = 0
limit = 3

for i in range(5):
    if requests < limit:
        requests += 1
        print("Request allowed")
    else:
        print("Too many requests")
A
Too many requests
Too many requests
Too many requests
Too many requests
Too many requests
B
Request allowed
Request allowed
Too many requests
Too many requests
Too many requests
C
Request allowed
Request allowed
Request allowed
Too many requests
Too many requests
D
Request allowed
Request allowed
Request allowed
Request allowed
Request allowed
Attempts:
2 left
💡 Hint

Count how many times the request is allowed before the limit is reached.

🔧 Debug
advanced
2:00remaining
How does this rate limiting code handle excess requests?

Look at this code snippet that tries to limit requests to 2 per user:

user_requests = {}
limit = 2

requests = ["user1", "user1", "user1"]

for user in requests:
    if user_requests.get(user, 0) < limit:
        user_requests[user] = user_requests.get(user, 0) + 1
        print(f"Request from {user} allowed")
    else:
        print(f"Request from {user} blocked")

What is the output?

Rest API
user_requests = {}
limit = 2

requests = ["user1", "user1", "user1"]

for user in requests:
    if user_requests.get(user, 0) < limit:
        user_requests[user] = user_requests.get(user, 0) + 1
        print(f"Request from {user} allowed")
    else:
        print(f"Request from {user} blocked")
A
Request from user1 allowed
Request from user1 allowed
Request from user1 allowed - It fails because the count is updated after the check.
BSyntaxError due to missing colon in if statement.
C
Request from user1 blocked
Request from user1 blocked
Request from user1 blocked - It blocks all requests.
D
Request from user1 allowed
Request from user1 allowed
Request from user1 blocked - It works correctly.
Attempts:
2 left
💡 Hint

Check how the count is updated and compared before allowing requests.

📝 Syntax
advanced
1:30remaining
Identify the syntax error in this rate limiting code snippet

Find the syntax error in this Python code that tries to implement rate limiting:

limit = 5
requests = 0

while requests <= limit
    print("Request allowed")
    requests += 1
Rest API
limit = 5
requests = 0

while requests <= limit
    print("Request allowed")
    requests += 1
AMissing colon ':' after the while condition.
BIndentation error on the print statement.
CUsing '&lt;=' instead of '&lt;' in the while condition.
DMissing parentheses in the print statement.
Attempts:
2 left
💡 Hint

Check the syntax of the while loop header.

🚀 Application
expert
2:30remaining
How many requests will be allowed in this sliding window rate limiter?

This code implements a sliding window rate limiter allowing 3 requests per 10 seconds:

import time

class SlidingWindowLimiter:
    def __init__(self, limit, window):
        self.limit = limit
        self.window = window
        self.timestamps = []

    def allow_request(self):
        now = time.time()
        self.timestamps = [t for t in self.timestamps if now - t < self.window]
        if len(self.timestamps) < self.limit:
            self.timestamps.append(now)
            return True
        return False

limiter = SlidingWindowLimiter(3, 10)

results = []
for _ in range(5):
    results.append(limiter.allow_request())
    time.sleep(1)

print(results)

What will be printed?

Rest API
import time

class SlidingWindowLimiter:
    def __init__(self, limit, window):
        self.limit = limit
        self.window = window
        self.timestamps = []

    def allow_request(self):
        now = time.time()
        self.timestamps = [t for t in self.timestamps if now - t < self.window]
        if len(self.timestamps) < self.limit:
            self.timestamps.append(now)
            return True
        return False

limiter = SlidingWindowLimiter(3, 10)

results = []
for _ in range(5):
    results.append(limiter.allow_request())
    time.sleep(1)

print(results)
A[True, True, True, True, True]
B[True, True, True, False, False]
C[False, False, False, False, False]
D[True, False, True, False, True]
Attempts:
2 left
💡 Hint

Think about how the sliding window removes timestamps older than 10 seconds.