Bird
Raised Fist0
Rest APIprogramming~20 mins

Token bucket algorithm in Rest API - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
πŸŽ–οΈ
Token Bucket Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of token bucket refill calculation
Consider a token bucket with capacity 10 tokens and refill rate 2 tokens per second. Initially, the bucket is full. After 3 seconds, how many tokens are in the bucket if 5 tokens were consumed immediately after start?
Rest API
capacity = 10
refill_rate = 2  # tokens per second
initial_tokens = 10
consumed = 5
elapsed_time = 3

# Calculate tokens after elapsed time
new_tokens = min(capacity, initial_tokens - consumed + refill_rate * elapsed_time)
print(new_tokens)
A10
B9
C11
D7
Attempts:
2 left
πŸ’‘ Hint
Remember the bucket cannot hold more tokens than its capacity.
🧠 Conceptual
intermediate
1:30remaining
Understanding token bucket burst capacity
Which statement best describes the burst capacity in a token bucket algorithm?
AThe maximum number of tokens that can be consumed instantly without waiting.
BThe rate at which tokens are added to the bucket per second.
CThe minimum number of tokens required to start the bucket.
DThe time interval between token refills.
Attempts:
2 left
πŸ’‘ Hint
Think about how many requests can be handled at once without delay.
πŸ”§ Debug
advanced
1:30remaining
Identify the error in token bucket token consumption code
What error will this code produce when trying to consume tokens from the bucket? ```python capacity = 5 tokens = 3 consume = 4 if tokens >= consume: tokens -= consume else: print("Not enough tokens") ```
ANo error, tokens become -1
BPrints 'Not enough tokens' and tokens remain 3
CRaises a TypeError
DSyntaxError due to missing colon
Attempts:
2 left
πŸ’‘ Hint
Check the condition and what happens when tokens are insufficient.
πŸ“ Syntax
advanced
1:30remaining
Syntax error in token bucket refill function
Which option contains the correct syntax for a Python function that refills tokens in a bucket?
Rest API
def refill_tokens(current_tokens, capacity, refill_rate, elapsed_time):
    new_tokens = current_tokens + refill_rate * elapsed_time
    if new_tokens > capacity:
        return capacity
    return new_tokens
A
def refill_tokens(current_tokens, capacity, refill_rate, elapsed_time):
    new_tokens = current_tokens + refill_rate * elapsed_time
    if new_tokens > capacity:
    return capacity
    return new_tokens
B
def refill_tokens(current_tokens, capacity, refill_rate, elapsed_time)
    new_tokens = current_tokens + refill_rate * elapsed_time
    if new_tokens > capacity:
        return capacity
    return new_tokens
C
def refill_tokens(current_tokens, capacity, refill_rate, elapsed_time):
    new_tokens = current_tokens + refill_rate * elapsed_time
    if new_tokens > capacity
        return capacity
    return new_tokens
D
def refill_tokens(current_tokens, capacity, refill_rate, elapsed_time):
    new_tokens = current_tokens + refill_rate * elapsed_time
    if new_tokens > capacity:
        return capacity
    return new_tokens
Attempts:
2 left
πŸ’‘ Hint
Check for colons and indentation.
πŸš€ Application
expert
3:00remaining
Calculate tokens after multiple consumption and refill events
A token bucket has capacity 8 tokens and refills at 1 token per second. Initially, it is full. The following events happen: - At 0 seconds, 3 tokens are consumed. - At 2 seconds, 4 tokens are consumed. - At 5 seconds, no consumption. What is the number of tokens in the bucket at 5 seconds?
A4
B7
C6
D5
Attempts:
2 left
πŸ’‘ Hint
Calculate tokens after each event considering refill and consumption.

Practice

(1/5)
1.

What is the main purpose of the token bucket algorithm in REST APIs?

easy
A. To encrypt API responses
B. To store user data securely
C. To control the rate of incoming requests by using tokens
D. To manage database connections

Solution

  1. Step 1: Understand the token bucket algorithm concept

    The token bucket algorithm limits how many requests can be processed by controlling tokens that refill over time.
  2. Step 2: Identify the purpose in REST APIs

    It helps prevent too many requests at once, protecting the server from overload.
  3. Final Answer:

    To control the rate of incoming requests by using tokens -> Option C
  4. Quick Check:

    Token bucket controls request rate = C [OK]
Hint: Token bucket limits request rate using tokens [OK]
Common Mistakes:
  • Confusing token bucket with data storage
  • Thinking it encrypts data
  • Assuming it manages database connections
2.

Which of the following is the correct way to represent a token bucket refill rate in pseudocode?

1. refill_rate = tokens_per_second
2. refill_rate = seconds_per_token
3. refill_rate = max_tokens * time
4. refill_rate = tokens / max_tokens
easy
A. refill_rate = seconds_per_token
B. refill_rate = tokens_per_second
C. refill_rate = max_tokens * time
D. refill_rate = tokens / max_tokens

Solution

  1. Step 1: Understand refill rate meaning

    The refill rate is how many tokens are added per second to the bucket.
  2. Step 2: Match with options

    refill_rate = tokens_per_second correctly shows tokens added per second, which is the refill rate.
  3. Final Answer:

    refill_rate = tokens_per_second -> Option B
  4. Quick Check:

    Refill rate = tokens per second [OK]
Hint: Refill rate means tokens added each second [OK]
Common Mistakes:
  • Confusing refill rate with time per token
  • Multiplying max tokens by time incorrectly
  • Using ratios instead of rates
3.

Given a token bucket with max_tokens = 5, refill_rate = 1 token/second, and an empty bucket at time 0, what is the number of tokens available at time 3 seconds?

medium
A. 3 tokens
B. 5 tokens
C. 0 tokens
D. 1 token

Solution

  1. Step 1: Calculate tokens refilled after 3 seconds

    Since refill rate is 1 token per second, after 3 seconds, 3 tokens are added.
  2. Step 2: Check max tokens limit

    The bucket max is 5 tokens, so 3 tokens fit without exceeding the max.
  3. Final Answer:

    3 tokens -> Option A
  4. Quick Check:

    3 seconds * 1 token/sec = 3 tokens [OK]
Hint: Multiply seconds by refill rate, cap at max tokens [OK]
Common Mistakes:
  • Assuming bucket fills instantly to max
  • Ignoring max token limit
  • Using refill rate incorrectly
4.

Consider this pseudocode snippet for token bucket check:
if tokens <= 0:
Β Β reject_request()
else:
Β Β tokens -= 1
Β Β allow_request()

What is the bug in this logic?

medium
A. It should check if tokens > 0 before allowing
B. It should increase tokens instead of decreasing
C. It should reject when tokens > 0
D. It should check if tokens < 1, not <= 0

Solution

  1. Step 1: Recall proper token bucket logic

    To consume 1 token, check if tokens >= 1 before decrementing (equivalent to reject if tokens < 1).
  2. Step 2: Identify the bug

    The code rejects only if tokens <= 0. For fractional tokens (common in real implementations), if 0 < tokens < 1, it allows the request, decrementing to negative, which is incorrect.
  3. Final Answer:

    It should check if tokens < 1, not <= 0 -> Option D
  4. Quick Check:

    Reject if tokens < 1 [OK]
Hint: Allow only if tokens >= 1 [OK]
Common Mistakes:
  • Using <= 0 instead of < 1 causes off-by-one errors
  • Increasing tokens on request instead of decreasing
  • Rejecting requests when tokens are available
5.

You want to implement a token bucket that allows bursts of up to 10 requests and refills tokens at 2 tokens per second. If a client sends 15 requests instantly after being idle for 3 seconds, how many requests will be allowed immediately?

hard
A. 6 requests
B. 5 requests
C. 15 requests
D. 10 requests

Solution

  1. Step 1: Calculate tokens available after 3 seconds idle

    Refill rate is 2 tokens/second, so after 3 seconds: 2 * 3 = 6 tokens. Max tokens allowed is 10, so bucket fills to 6 tokens.
  2. Step 2: Consider burst capacity

    Since the bucket max is 10, if it was full before idle, it would have 10 tokens. But starting empty, after 3 seconds it has 6 tokens.
  3. Step 3: Determine allowed requests

    The client sends 15 requests instantly, but only 6 tokens are available, so only 6 requests allowed immediately.
  4. Final Answer:

    6 requests -> Option A
  5. Quick Check:

    3 sec * 2 tokens/sec = 6 tokens available [OK]
Hint: Tokens = min(max_tokens, refill_rate * idle_time) [OK]
Common Mistakes:
  • Assuming bucket always full at max tokens
  • Allowing more requests than tokens available
  • Ignoring refill rate and idle time