0Pricing
Coding Interview Prep · Lesson

Queues and collections.deque

Push and pop from both ends fast.

Queues and collections.deque is a free Coding Interview Prep lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Coding Interview Prep learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

First In, First Out

A queue serves items in the order they arrived, like a line at a shop. The first one in is the first one out.

Why Not Use a List

A list can pop from the front, but pop(0) is O(n) because every other item shifts left. That is too slow for big inputs.

q = []
q.pop(0)  # O(n), avoid this

Meet collections.deque

The deque from collections is a double-ended queue that adds and removes from both ends in O(1). It is your contest go-to.

from collections import deque
q = deque()

Enqueue at the Back

Add new items to the right end with append, exactly like a list. This is the back of the queue.

q.append(1)
q.append(2)

Dequeue from the Front

Remove the oldest item from the left with popleft, which runs in constant time and gives true FIFO behavior.

first = q.popleft()  # returns 1

Both Ends Are Open

A deque also supports appendleft and pop from the right. That flexibility lets one structure act as a stack or a queue.

q.appendleft(0)
last = q.pop()

Check Before You Pop

Removing from an empty deque raises an error, so test while q in loops to keep your traversal safe.

while q:
    x = q.popleft()

Queues Power BFS

The most common contest use is BFS. You enqueue a start node, then keep popping the front and pushing its neighbors.

A Tiny BFS Skeleton

This loop visits nodes layer by layer. Each neighbor is appended and later processed in arrival order.

while q:
    node = q.popleft()
    for nb in graph[node]:
        q.append(nb)

Bound the Deque Size

Passing maxlen makes a deque drop the oldest item when full, perfect for sliding windows and recent-history tracking.

window = deque(maxlen=3)

One Structure, Many Roles

Remember that deque is fast on both ends, so reach for it whenever you need a queue, a stack, or a sliding buffer.

Quick Check

You need fast front removal in a queue. Which choice is right?

Recap: Deque Is the Fast Queue

You met collections.deque: append and popleft for O(1) FIFO, both ends open, and maxlen for windows. It is the backbone of BFS. 🎯

Frequently asked questions

Is the “Queues and collections.deque” lesson free?

Yes — the full text of “Queues and collections.deque” is free to read here on the web, and the Coding Interview Prep course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Coding Interview Prep course, upgrade to CoddyKit PRO.

What will I learn in “Queues and collections.deque”?

Push and pop from both ends fast. You practise Coding Interview Prep with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Coding Interview Prep?

No prior experience is required. Coding Interview Prep on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Queues and collections.deque” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Coding Interview Prep lesson?

Yes. Every Coding Interview Prep lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Stacks for Matching Brackets
  2. Monotonic Stack: Next Greater Element
  3. Queues and collections.deque
  4. Sliding Window Maximum with Deque
← Back to Coding Interview Prep