00:00

Deque Data Structure

Last updated:

What is Deque in Data Structure?

Deque (pronounced "deck") is short for double-ended queue.

A deque is a linear collection that allows elements to be inserted and removed at both ends: the front and the rear. An ordinary queue only adds at the rear and removes at the front; a stack only works at one end. A deque can do all of these.

This makes it useful whenever elements must be taken from either end, for example in sliding-window algorithms, undo histories with a size limit, or work-stealing schedulers.

The following diagram shows a deque.

Deque: elements can be inserted and removed at both the front and the rear

Because both ends are open, a deque is not tied to one order: used from one end it behaves as a stack (LIFO), and used by adding at one end and removing at the other it behaves as a queue (FIFO).

Types of Deque in Data Structure

Restricting one of the ends gives two special kinds of deque:

  1. Input restricted queue
  2. Output restricted queue

They are described below.

  • Input restricted queue: insertion is allowed at only one end, while deletion is allowed at both ends.

    In the diagram, elements are inserted only at the rear and deleted from both ends.

    Input restricted queue: insertion at the rear only, deletion at both front and rear
  • Output restricted queue: deletion is allowed at only one end, while insertion is allowed at both ends.

    In the diagram, elements are inserted at both ends and deleted only at the rear.

    Output restricted queue: insertion at both front and rear, deletion at the rear only

Operations performed on Deque

A deque supports four core operations:

  1. Insertion at front
  2. Insertion at rear
  3. Deletion at front
  4. Deletion at rear

and usually these helper operations as well:

  1. Get the front element
  2. Get the rear element
  3. Check whether the deque is full (only for a fixed-capacity deque)
  4. Check whether the deque is empty

Example: Deque Implementation in Java

The program below stores the deque in a circular array, the same idea that java.util.ArrayDeque uses. It keeps the index of the first element (front) and the number of elements (size); the rear index is calculated from them. Inserting at the front steps front back by one position, wrapping from 0 to the last index, so every operation is O(1). The class is called IntDeque to avoid confusion with the java.util.Deque interface.

ArrayDequeDemo.java
// A fixed-capacity deque of int values stored in a circular array
class IntDeque {
  private final int[] items;
  private int front; // index of the first element
  private int size;  // number of elements stored

  IntDeque(int capacity) {
    items = new int[capacity];
    front = 0;
    size = 0;
  }

  boolean isEmpty() {
    return size == 0;
  }

  boolean isFull() {
    return size == items.length;
  }

  // Index of the last element (valid only when the deque is not empty)
  private int rearIndex() {
    return (front + size - 1) % items.length;
  }

  // Adds an element before the current front: O(1)
  void insertFront(int value) {
    if (isFull()) {
      System.out.println("Overflow: cannot insert " + value);
      return;
    }
    front = (front - 1 + items.length) % items.length; // step back, wrapping
    items[front] = value;
    size++;
  }

  // Adds an element after the current rear: O(1)
  void insertRear(int value) {
    if (isFull()) {
      System.out.println("Overflow: cannot insert " + value);
      return;
    }
    items[(front + size) % items.length] = value;
    size++;
  }

  // Removes the front element: O(1)
  void deleteFront() {
    if (isEmpty()) {
      System.out.println("Underflow: deque is empty");
      return;
    }
    front = (front + 1) % items.length;
    size--;
  }

  // Removes the rear element: O(1)
  void deleteRear() {
    if (isEmpty()) {
      System.out.println("Underflow: deque is empty");
      return;
    }
    size--;
  }

  int getFront() {
    if (isEmpty()) {
      throw new IllegalStateException("Deque is empty");
    }
    return items[front];
  }

  int getRear() {
    if (isEmpty()) {
      throw new IllegalStateException("Deque is empty");
    }
    return items[rearIndex()];
  }
}

public class ArrayDequeDemo {
  public static void main(String[] args) {
    IntDeque deque = new IntDeque(5);

    deque.insertRear(10);
    deque.insertRear(20);
    System.out.println("Rear element: " + deque.getRear());

    deque.deleteRear();
    System.out.println("After deleteRear, rear element: " + deque.getRear());

    deque.insertFront(30);
    deque.insertFront(40);
    System.out.println("Front element: " + deque.getFront());

    deque.deleteFront();
    System.out.println("After deleteFront, front element: " + deque.getFront());
    System.out.println("Rear element: " + deque.getRear());
  }
}
Output
Rear element: 20
After deleteRear, rear element: 10
Front element: 40
After deleteFront, front element: 30
Rear element: 10

Time and Space Complexity of Deque Operations

OperationTime ComplexityExtra Space
Insert at front / rearO(1) (amortized O(1) if the array has to grow, as in ArrayDeque)O(1)
Delete at front / rearO(1)O(1)
Get front / rearO(1)O(1)
Search for a valueO(n)O(1)

The deque itself needs O(n) memory for n elements (O(capacity) for a fixed array).

Advantages of Deque

  • Insertion and deletion at both ends in O(1) time.
  • One structure can serve as a stack, a queue, or both; Java recommends ArrayDeque for both roles.
  • Makes some algorithms simple and linear-time, such as the sliding-window maximum (FAQ Q6).
  • Growable implementations (ArrayDeque, LinkedList) have no fixed capacity.

Disadvantages of Deque

  • No efficient access to the middle: reaching or searching for an element there is O(n).
  • A fixed-capacity array deque can overflow, and the wrap-around index arithmetic is easy to get wrong.
  • A linked-list deque needs two references per node, so it uses more memory than an array-based one.
  • ArrayDeque and LinkedList are not thread-safe; concurrent code needs ConcurrentLinkedDeque or LinkedBlockingDeque.