Pick the fix
You must repeatedly remove items from the front of a long queue. Which choice is right?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A collections.deque with popleft()
Right: O(1) at both ends.
Picking the structure by its cheap operations is the point; deque gives queue behaviour without writing a chain yourself.
Why not the others?
- B A
listwithpop(0). Each call shifts every remaining item. - C Sort the list first. Sorting does not change the cost of removing from the front.