← Quiz: Design a Rate Limiter: all 15 questions
Quiz: Design a Rate Limiter · Question 7 of 15
What does this print? (code)
What does this print?
from collections import deque
log, res = deque(), []
for t in [0, 1, 2, 3, 11]:
while log and log[0] <= t - 10:
log.popleft()
if len(log) < 3:
log.append(t)
res.append(t)
print(res)
- A.[0, 1, 2, 3, 11]
- B.[0, 1, 2]
- C.[0, 1, 2, 11]
- D.[0, 1, 11]
Answer & explanation
Correct answer: [0, 1, 2, 11]
At t=3 the window already holds 3 requests. At t=11 the requests from t=0 and t=1 have expired, so 11 is allowed.
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 →