Skip to content

Deque Visualizer

Add and remove at both ends of a double-ended queue and watch the ring buffer underneath.

Size: 0 Capacity: 8 front: 0 rear: −1

A deque (double-ended queue) lets you add and remove at both ends in O(1), using a ring buffer.

Step 1 of 1

// Deque on a ring buffer of size NpushFront(x):  if size = N: error "deque full"  front = (front − 1 + N) mod N; a[front] = x; size += 1pushBack(x):  if size = N: error "deque full"  rear = (rear + 1) mod N; a[rear] = x; size += 1popFront():  if size = 0: error "deque empty"  x = a[front]; front = (front + 1) mod N; size −= 1popBack():  if size = 0: error "deque empty"  x = a[rear]; rear = (rear − 1 + N) mod N; size −= 1
Deque operations
OperationTime
Push frontO(1)
Push backO(1)
Pop frontO(1)
Pop backO(1)
Access by indexO(1) on a ring buffer

A deque (pronounced “deck”) is a double-ended queue: you can push and pop at the front and at the back. It works as a stack, a queue or both at once. This visualizer builds a deque on a ring buffer of 8 slots and shows it as a ring and as an array, with the front and rear pointers moving in both directions.

Push front moves front one step backwards, wrapping from 0 to 7; push back moves rear forwards. Every operation is animated with the index arithmetic written out, and all four run in O(1) time.

How to use the Deque Visualizer tool

  1. Type a value and press Push front or Push back.
  2. Press Pop front or Pop back to remove from either end.
  3. Watch front move backwards and rear move forwards around the ring.
  4. Use Step and Back to check each index calculation.

Why use ToolOrca Deque Visualizer

  • Push and pop at both ends
  • Ring view and array view
  • Wrap-around index arithmetic shown
  • Full and empty checks
  • Pseudocode highlighted for every step
  • Code in C++, Java, Python and JavaScript

How a deque works

A deque supports pushFront, pushBack, popFront and popBack, usually with peekFront and peekBack too. On a ring buffer of size N, pushFront does front = (front − 1 + N) mod N and writes there, while pushBack does rear = (rear + 1) mod N. Adding N before taking the modulo keeps the index from going negative.

A deque can also be built as a doubly linked list. The ring buffer version uses less memory and is faster in practice, so Java’s ArrayDeque, Python’s collections.deque (blocks of slots) and C++ std::deque (chunks of arrays) all avoid one node per element.

When to use a deque

Use a deque when you need both stack and queue behaviour: the sliding window maximum problem, undo history with a size limit, work-stealing schedulers, palindrome checks and 0-1 BFS in graphs. In Java, ArrayDeque is the recommended class for both stacks and queues.

Every visualizer on this site runs in your browser with the same controls: Play, Pause, Step forward, Step back and a speed slider. You can also drag the position slider to jump to any step, or focus the drawing and use the Space bar and arrow keys.

Frequently asked questions

What is a deque?

A double-ended queue: a sequence where you can add and remove items at both the front and the back.

What is the time complexity of deque operations?

Push and pop at either end are O(1). On a ring buffer, access by index is O(1) too.

Is a deque a stack or a queue?

Both. Use only one end and it is a stack; push at one end and pop at the other and it is a queue.

Which deque should I use in Python or Java?

Python: collections.deque with append, appendleft, pop and popleft. Java: ArrayDeque with addFirst, addLast, pollFirst and pollLast.