Bird
Raised Fist0

In a rate limiting system, a developer notices clients are blocked even after waiting the reset time. The code snippet:

medium📝 Debug Q7 of Q15
Rest API - Rate Limiting and Throttling
In a rate limiting system, a developer notices clients are blocked even after waiting the reset time. The code snippet:
if requests >= limit:
  reject request
else:
  process request
requests += 1
if time_passed >= reset_time:
  requests = 0

What is the likely bug?
AThe requests counter resets after increment, causing off-by-one errors.
BThe reset condition is checked after increment, so requests may exceed limit before reset.
CThe limit variable is not defined.
DThe reject request line is inside the else block.
Step-by-Step Solution
Solution:
  1. Step 1: Review order of operations

    Requests increment before reset check, so requests can exceed limit before reset.
  2. Step 2: Identify fix

    Reset check should happen before increment to avoid blocking after reset time.
  3. Final Answer:

    The reset condition is checked after increment, so requests may exceed limit before reset. -> Option B
  4. Quick Check:

    Reset after increment causes blocking = B [OK]
Quick Trick: Check reset before incrementing requests to avoid blocking [OK]
Common Mistakes:
MISTAKES
  • Ignoring order of reset and increment
  • Assuming limit variable missing
  • Misplacing reject inside else block

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Rest API Quizzes