Challenge - 5 Problems
Token Bucket Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Remember the bucket cannot hold more tokens than its capacity.
✗ Incorrect
After consuming 5 tokens, 5 remain. After 3 seconds, 2 tokens per second refill adds 6 tokens. 5 + 6 = 11, but capacity is 10, so final tokens = 10.
🧠 Conceptual
intermediate1:30remaining
Understanding token bucket burst capacity
Which statement best describes the burst capacity in a token bucket algorithm?
Attempts:
2 left
💡 Hint
Think about how many requests can be handled at once without delay.
✗ Incorrect
Burst capacity is the bucket's maximum token count, allowing that many requests to be served immediately.
🔧 Debug
advanced1: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")
```
Attempts:
2 left
💡 Hint
Check the condition and what happens when tokens are insufficient.
✗ Incorrect
Since tokens (3) are less than consume (4), the else branch runs, printing the message and tokens stay unchanged.
📝 Syntax
advanced1: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
Attempts:
2 left
💡 Hint
Check for colons and indentation.
✗ Incorrect
Option D has correct function definition, colons, and proper indentation.
🚀 Application
expert3: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?
Attempts:
2 left
💡 Hint
Calculate tokens after each event considering refill and consumption.
✗ Incorrect
Start: 8 tokens
At 0s consume 3 → tokens = 5
Refill 2s *1 = 2 tokens → tokens = min(8,5+2)=7
At 2s consume 4 → tokens = 3
Refill 3s *1 = 3 tokens → tokens = min(8,3+3)=6
At 5s no consumption, tokens = 6
But careful: consumption at 2s happens after refill at 2s? Usually consumption happens at event time, so refill from 0 to 2s is 2 tokens, then consume 4 tokens.
Tokens at 2s after refill: 7, after consumption: 3
From 2s to 5s refill 3 tokens → 3+3=6 tokens at 5s.
So final tokens = 6.