Bird
0
0

Identify the error in this token bucket refill code snippet:

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

Identify the error in this token bucket refill code snippet:

def refill_tokens(bucket):
  bucket.tokens += bucket.refill_rate
  if bucket.tokens > bucket.max_tokens:
    bucket.tokens = bucket.refill_rate

ANot increasing tokens by refill_rate
BMissing return statement
CResetting tokens to refill_rate instead of max_tokens
DUsing wrong comparison operator
Step-by-Step Solution
Solution:
  1. Step 1: Analyze token reset logic

    If tokens exceed max_tokens, they should be set to max_tokens, not refill_rate.
  2. Step 2: Identify correct fix

    Change assignment to bucket.tokens = bucket.max_tokens to fix the error.
  3. Final Answer:

    Resetting tokens to refill_rate instead of max_tokens -> Option C
  4. Quick Check:

    Reset to max_tokens when overflowing [OK]
Quick Trick: Reset tokens to max, not refill rate [OK]
Common Mistakes:
  • Setting tokens to refill_rate
  • Ignoring max_tokens limit
  • Wrong operator usage

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Rest API Quizzes