Medium · Streams & I/O

Rate limiter iterator

Given a list of values, a refill rate r (tokens per second) and a burst capacity b, 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

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More Streams & I/O problems