Bird
Raised Fist0

Identify the logical error in this per-IP rate limiting pseudocode:

medium📝 Debug Q7 of Q15
Rest API - Rate Limiting and Throttling
Identify the logical error in this per-IP rate limiting pseudocode:
requests = {"123.45.67.89": 4}
limit = 5
ip = "123.45.67.89"
if requests[ip] > limit:
    block()
else:
    allow()
requests[ip] += 1
AIncrementing requests[ip] after the check causes off-by-one errors
BUsing > instead of >= in the if condition is incorrect
CThe IP address key is missing from the requests dictionary
DThe limit variable should be a dictionary, not a single value
Step-by-Step Solution
Solution:
  1. Step 1: Review the order of operations

    The request count is checked before incrementing, so the current request isn't counted.
  2. Step 2: Identify the impact

    This causes the system to allow one extra request beyond the limit.
  3. Final Answer:

    Incrementing requests[ip] after the check causes off-by-one errors -> Option A
  4. Quick Check:

    Increment before checking to avoid off-by-one [OK]
Quick Trick: Increment count before limit check [OK]
Common Mistakes:
MISTAKES
  • Checking limit before incrementing count
  • Confusing > with >= in conditions
  • Assuming limit is per IP but using a single value

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Rest API Quizzes