Medium · Streams & I/O

Rate limiter iterator

Given a list of values, a positive refill rate r (tokens per second) and a burst capacity b ≥ 1, implement an iterator: hasNext() reports whether values remain, and next() returns the next value in order, first waiting as long as needed so that a token bucket is never overdrawn. The bucket starts full with b tokens, gains r tokens per elapsed second up to b, and each returned value spends one token.

Examples

Example 1

burst=2, rate=1, step=0.5s

Output: [1, 2, 3, 4, 5]

Example 2

burst=1, rate=2, step=0.25s

Output: [10, 20, 30]

Rebuild it in the studio

Open every published interview problem, solution, scene, and memory room free, with no account.

More Streams & I/O problems