Bird
0
0

This code snippet tries to implement rate limiting but has a bug:

medium📝 Debug Q14 of 15
Rest API - Rate Limiting and Throttling
This code snippet tries to implement rate limiting but has a bug:
requests = 0
limit = 2
for req in requests_list:
    if requests > limit:
        reject(req)
    else:
        process(req)
        requests += 1
What is the bug?
AThe condition should be requests < limit, not requests > limit
BThe requests counter is not incremented
CThe loop variable name conflicts with requests
DThe limit value is too high
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the if condition logic

    The code rejects requests when requests > limit, but it should allow requests while requests < limit.
  2. Step 2: Understand correct rate limiting condition

    To process requests up to the limit, the condition must check if requests < limit before processing.
  3. Final Answer:

    The condition should be requests < limit, not requests > limit -> Option A
  4. Quick Check:

    Process if requests < limit [OK]
Quick Trick: Check if condition matches 'less than limit' to process [OK]
Common Mistakes:
  • Using greater than instead of less than in condition
  • Forgetting to increment requests counter
  • Confusing variable names in loop

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Rest API Quizzes