All problems
Leaky Bucket Level
Problem
Simulate a leaky bucket that drains at a constant rate.
capacity is the maximum level, leak_rate is the units drained per time unit, and operations contains "add" (values[i] units at times[i]) or "level" (current level at times[i]). Between events the level drops by leak_rate times the elapsed time, never below zero, and an add caps the level at capacity.
Return the list of recorded results (None for add, an integer for level).
Hint
Drain before processing each event, then cap any addition at capacity.
Test cases
case 1
in: [10,2,["add","level","level","add","level"],[5,0,0,8,0],[0,1,3,4,6]]
expected: [null,3,0,null,4]
case 2
in: [10,2,["level"],[0],[5]]
expected: [0]
case 3
in: [5,1,["add","add","level"],[3,3,0],[0,0,0]]
expected: [null,null,5]
case 4
in: [3,1,["add","level","level"],[5,0,0],[0,0,2]]
expected: [null,3,1]
Starter codePython
def leaky_bucket(capacity, leak_rate, operations, values, times):
# Your code here
pass4 test cases · run your code against them in the editor