0
0
Prompt Engineering / GenAIml~20 mins

Rate limiting and abuse prevention in Prompt Engineering / GenAI - Practice Problems & Coding Challenges

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:00remaining
Understanding Rate Limiting Purpose
Why is rate limiting important in AI service APIs?
ATo increase the speed of AI model training
BTo prevent excessive use that can overload servers and degrade service quality
CTo improve the accuracy of AI predictions by limiting data input
DTo reduce the size of AI models for faster downloads
Attempts:
2 left
💡 Hint
Think about what happens when too many requests come at once.
Model Choice
intermediate
1:30remaining
Choosing a Rate Limiting Strategy
Which rate limiting strategy allows a user to make bursts of requests but limits the average rate over time?
AFixed window limiting
BNo rate limiting
CToken bucket algorithm
DLeaky bucket algorithm
Attempts:
2 left
💡 Hint
This algorithm uses tokens to allow bursts but controls the overall rate.
Predict Output
advanced
2:00remaining
Output of Rate Limiting Code Snippet
What is the output of this Python code simulating a fixed window rate limiter?
Prompt Engineering / GenAI
import time

class FixedWindowLimiter:
    def __init__(self, limit, window_seconds):
        self.limit = limit
        self.window_seconds = window_seconds
        self.request_times = []

    def allow_request(self):
        current_time = time.time()
        # Remove requests outside the current window
        self.request_times = [t for t in self.request_times if t > current_time - self.window_seconds]
        if len(self.request_times) < self.limit:
            self.request_times.append(current_time)
            return True
        else:
            return False

limiter = FixedWindowLimiter(limit=3, window_seconds=5)
results = []
for _ in range(5):
    results.append(limiter.allow_request())
    time.sleep(1)
print(results)
A[True, True, False, False, False]
B[False, False, False, False, False]
C[True, True, True, True, False]
D[True, True, True, False, False]
Attempts:
2 left
💡 Hint
The limiter allows 3 requests per 5 seconds window.
Metrics
advanced
1:30remaining
Evaluating Abuse Prevention Effectiveness
Which metric best measures how well a rate limiter prevents abuse without blocking legitimate users?
AFalse positive rate (blocking good users)
BAverage response time of the server
CNumber of API endpoints
DModel accuracy on training data
Attempts:
2 left
💡 Hint
Think about mistakenly blocking users who should be allowed.
🔧 Debug
expert
2:00remaining
Debugging Token Bucket Implementation
Given this token bucket rate limiter code, what error will occur when calling allow_request() repeatedly without waiting?
Prompt Engineering / GenAI
import time

class TokenBucket:
    def __init__(self, capacity, refill_rate):
        self.capacity = capacity
        self.tokens = capacity
        self.refill_rate = refill_rate
        self.last_refill = 0

    def allow_request(self):
        current_time = time.time()
        elapsed = current_time - self.last_refill
        self.tokens = min(self.capacity, self.tokens + elapsed * self.refill_rate)
        self.last_refill = current_time
        if self.tokens >= 1:
            self.tokens -= 1
            return True
        else:
            return False

bucket = TokenBucket(5, 1)
for _ in range(10):
    print(bucket.allow_request())
ANameError because time module is not imported
BValueError due to negative tokens
CNo error, prints True for first 5 calls then False
DZeroDivisionError in refill calculation
Attempts:
2 left
💡 Hint
Check if all required modules are imported.