Fill the blank
Fill the blank so old times leave the window.
from collections import deque
w = 3000
recent: deque[int] = deque()
out = []
for t in [1, 100, 3001, 3002]:
recent.append(t)
while recent[0] < t - w:
____
out.append(len(recent))
print(out)Choose an answer, then open Show answer below to check it.
Show answer
Answer: A recent.popleft()
Right: the oldest time sits at the front of the queue.
At t = 3002 the request at time 1 falls out of the window, which is why the last count is 3.
Why not the others?
- B recent.pop(). That removes the newest time, which is still inside the window.
- C recent.clear(). That drops times that are still inside the window.