Step 1 of 4

Spend a token per request

Start with a bucket that never refills.

It begins full — capacity tokens. Each call to allow(now) spends one and returns true; when there are none left it returns false and spends nothing.

capacity 3
allow()  ->  true    allow()  ->  true
allow()  ->  true    allow()  ->  false

refill is stubbed out and tokens is written for you. Every test in this step happens at now = 0, so no time passes and the stub costs you nothing — the clock is step 2.

Your build

Running is free — Submit is what records the step. Or press ⌘↩

Tests

4 cases, 1 hidden
calltypeexpectedresult
runOps(3, 1, [["allow",0],["allow",0],["allow",0],["allow",0]])a full bucket allows exactly its capacity[true,true,true,false]
runOps(3, 1, [["tokens",0]])a new bucket reports itself full[3]
runOps(2, 1, [["allow",0],["tokens",0],["allow",0],["tokens",0]])tokens fall as requests are allowed[true,1,true,0]
withheldhiddenwithheld

Hidden cases run too — their inputs aren't listed here, so aim for a general solution rather than one fitted to the cases above.

Hints

Stuck? Hints open one at a time, each giving a little more away.

3 hints left