Skip to main content

Deque

What deque means, when inserting and removing from both ends works better than a simple stack or queue.

Andrews Ribeiro

Andrews Ribeiro

Founder & Engineer

What it is

A deque is a structure that lets you insert and remove from both the front and the back.

It sits somewhere between a queue and a stack in flexibility.

When to use it

It appears when the problem needs to:

  • operate from both ends
  • keep candidates inside a moving window
  • combine queue behavior with fast discard

Common mistake

The classic mistake is treating deque as an implementation detail with no algorithmic impact.

In many problems, the ability to work from both ends is exactly what makes the good solution possible.

Better question

Before using it, ask:

  1. do I need to remove from both the front and the back?
  2. does a simple queue or stack trap me?
  3. am I storing candidates that enter and leave from different sides?

Keep exploring