Back to All Algorithms
Double-Ended Queue (Deque)
Category: Data Structures | Time: O(1) | Space: O(n)
Visualization
Visual representation of the data structure
Enter commands to start deque visualization.
About Double-Ended Queue (Deque)

A Deque, or double-ended queue, is a generalized version of a Queue. Elements can be added or removed from either the front or the rear, providing maximum flexibility.

Code Editor
The code is for reference. Editing it won't affect the visualization.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Variable State
{
  "status": "Initializing deque"
}
Double-Ended Queue (Deque): How It Works
Last updated October 28, 2026

Overview

A deque (pronounced 'deck', short for double-ended queue) allows insertion and removal at both the front and the rear. It generalises both the stack (use one end) and the queue (add at one end, remove at the other).

Deques shine in sliding-window problems. To find the maximum of every window of size k, keep a deque of indices whose values are in decreasing order: drop indices from the front when they leave the window, and drop smaller values from the rear when a larger one arrives. Each index enters and leaves at most once, so the whole scan is O(n).

The default commands add 10 and 20 at the rear, add 5 at the front (giving [5, 10, 20]), remove from the rear (20) and remove from the front (5). Only 10 remains.

Step by step

  1. addFront(x) / addRear(x): insert at either end.
  2. removeFront() / removeRear(): remove from either end, handling the empty case.
  3. peekFront() / peekRear(): read either end without removing.

Complexity

CaseCostWhy
Add / remove at either endO(1)With a doubly linked list or circular buffer.
Access by indexO(1) or O(n)O(1) for array-backed deques, O(n) for linked ones.
SpaceO(n)One slot per element.

When to use it

  • Sliding window maximum/minimum problems.
  • Palindrome checks: compare and remove from both ends.
  • Work-stealing schedulers, where a thread takes work from its own end and others steal from the opposite end.
  • Undo histories with a size limit: drop the oldest entry from the front.

Common mistakes

  • The visualizer uses unshift for addFront, which is O(n) on a JavaScript array; a real deque uses a circular buffer.
  • Mixing up which end is the 'front' between functions.
  • Not checking for emptiness before removing from either end.
Worked Example: Double-Ended Queue (Deque) Dry Run
Generated from the same trace that drives the visualizer above, using the default input.

A dry run is the best way to check your understanding of Double-Ended Queue (Deque): follow each row, predict the next one, and compare. The run below takes 12 steps in total. The Line column refers to the line numbers in the code listing above.

Input
addRear 10,addRear 20,addFront 5,removeRear,removeFront
Result
status = Operations complete, final_deque = [10]
StepLineArrayWhat happens
11[]status = Initializing deque
28[]action = addRear(10)
39[10]status = Added 10 to rear, front = 10, rear = 10
48[10]action = addRear(20)
59[10, 20]status = Added 20 to rear, front = 10, rear = 20
65[10, 20]action = addFront(5)
76[5, 10, 20]status = Added 5 to front, front = 5, rear = 20
815[5, 10, 20]action = removeRear()
917[5, 10]status = Removed 20 from rear, front = 5, rear = 10
1011[5, 10]action = removeFront()
1113[10]status = Removed 5 from front, front = 10, rear = 10
1224[10]status = Operations complete, final_deque = [10]
Frequently Asked Questions
About Double-Ended Queue (Deque)

What is a Deque?

A Deque (pronounced 'deck'), or Double-Ended Queue, is a generalized version of a Queue. It's a linear collection of elements that allows insertion and deletion at both the front and the rear ends.

What are the primary operations of a Deque?

  • addFront: Adds an element to the beginning.
  • addRear: Adds an element to the end.
  • removeFront: Removes an element from the beginning.
  • removeRear: Removes an element from the end.

How is a Deque different from a Queue or a Stack?

A Deque is more flexible. You can think of a Stack as a deque where insertions and deletions only happen at one end, and a Queue as a deque where insertions happen at one end and deletions at the other. A deque supports all of these operations.

What is the time complexity of deque operations?

When implemented with a data structure like a doubly linked list or a dynamic array, all primary operations (`addFront`, `addRear`, `removeFront`, `removeRear`) can be performed in O(1) amortized time.

Where are deques used?

Deques are useful for algorithms that require a 'sliding window', like finding the maximum element in all subarrays of a fixed size. They are also used for implementing palindrome checkers and other algorithms that require access to both ends of a sequence.

Where can I learn more?

You can find a more detailed explanation on GeeksforGeeks.