Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to represent the token bucket refill logic.
HLD
tokens = min(capacity, tokens + [1] * elapsed_time)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using capacity instead of rate
Adding tokens without considering elapsed time
✗ Incorrect
The refill rate is multiplied by elapsed time to add tokens to the bucket.
2fill in blank
mediumComplete the code to check if a request can be allowed in the leaky bucket algorithm.
HLD
if queue_size < [1]: allow_request()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using rate instead of capacity
Ignoring queue size
✗ Incorrect
The queue size must be less than the bucket capacity to allow a new request.
3fill in blank
hardFix the error in the token bucket token consumption logic.
HLD
if tokens >= [1]: tokens -= request_size
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing tokens with capacity
Subtracting rate instead of request size
✗ Incorrect
We must check if tokens are enough to cover the request size before consuming.
4fill in blank
hardFill both blanks to implement the leaky bucket leak rate update.
HLD
leaked = min(queue_size, [1] * [2]) queue_size -= leaked
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using capacity instead of rate
Using tokens instead of elapsed time
✗ Incorrect
The leak amount is rate multiplied by elapsed time, limited by the current queue size.
5fill in blank
hardFill all three blanks to implement a token bucket request check and update.
HLD
if tokens >= [1]: tokens -= [2] allow = [3]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting allow to False incorrectly
Subtracting wrong value from tokens
✗ Incorrect
Check if tokens cover request size, subtract request size, and allow the request.