Bird
0
0

Identify the bug in this fixed window rate limiter code snippet and select the fix:

medium📝 Debug Q14 of 15
Rest API - Rate Limiting and Throttling

Identify the bug in this fixed window rate limiter code snippet and select the fix:

limit = 5
window_size = 60
count = 0
window_start = int(time.time())

if int(time.time()) - window_start > window_size:
    count = 0

count += 1
if count > limit:
    print("Blocked")
else:
    print("Allowed")
AIncrease limit to 10
BChange <code>count += 1</code> to <code>count = 1</code>
CRemove the if condition checking time difference
DMove <code>window_start</code> update inside the if block to reset window
Step-by-Step Solution
Solution:
  1. Step 1: Identify window reset logic

    The code resets count when window expires but does not update window_start, so window never moves forward.
  2. Step 2: Fix by updating window start time

    Inside the if block, update window_start = int(time.time()) to start a new window correctly.
  3. Final Answer:

    Move window_start update inside the if block to reset window -> Option D
  4. Quick Check:

    Reset count and window start together [OK]
Quick Trick: Update window start when resetting count [OK]
Common Mistakes:
  • Not updating window start time
  • Resetting count incorrectly
  • Ignoring time difference condition

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Rest API Quizzes