0
0
Rest APIprogramming~20 mins

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

Choose your learning style9 modes available
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.