A Queue with Deque

Talk it through out loud with your AI coach.

The problem

  • medium
  • Pro

A breadth-first search uses a Python list as its queue, adding items with append and taking them from the front with pop at index zero, and it's slow on large inputs. Explain why, how collections deque fixes it, and how deque can also keep just the last hundred log lines.

What a strong answer covers

  • Why taking items from the front of a list gets slow.
  • The operations deque makes cheap and which ones to use for a queue.
  • Using a size limit to keep only the most recent items.
  • What a deque is not good at compared with a list.

Common mistake: Saying lists are linked lists, which gets the cost of each operation the wrong way round.

Knowing this is half of it. Saying it clearly out loud is the part the coach helps you practise.

Practise it out loud