DSA Basics: Deque

Learn the double-ended queue, where both front and rear support insert and remove operations.

Back to Learn DSA

A deque is a double-ended queue.

Unlike a normal queue, a deque can add and remove values from both ends.

Core operations

Why deques are useful

A deque is flexible because it can behave like both a stack and a queue.

If you only use the back, it acts like a stack. If you add at the back and remove from the front, it acts like a queue.

Common use cases

Sliding window intuition

Deque problems often ask you to keep a useful window of values while moving through an array.

For example, in a sliding window maximum problem, the deque stores indexes that could still become the maximum.

Complexity

When backed by a proper deque implementation:

Use the visualizer below to operate on both ends.

Interactive walkthrough

Deque Visualizer

Add and remove values from both ends.