🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Design a Rate Limiter: all 15 questions

Quiz: Design a Rate Limiter · Question 2 of 15

What does this print? (code)

What does this print?

tokens, cap, rate, last = 2, 2, 1, 0
out = []
for t in [0, 0, 0, 1]:
    tokens = min(cap, tokens + (t - last) * rate)
    last = t
    if tokens >= 1:
        tokens -= 1
        out.append("Y")
    else:
        out.append("N")
print("".join(out))
  1. A.YYYY
  2. B.YYNY
  3. C.YYNN
  4. D.YNYN
Answer & explanation

Correct answer: YYNY

Two tokens serve the first two requests at t=0, the third finds the bucket empty, and one token has refilled by t=1.

Can you get the rest right without peeking?

Take the timed Quiz: Design a Rate Limiter quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →