Interview kitsBlog

Your dream job? Lets Git IT.
Interactive technical interview preparation platform designed for modern developers.

XGitHub

Platform

  • Categories

Resources

  • Blog
  • About the app
  • FAQ
  • Feedback

Legal

  • Privacy Policy
  • Terms of Service

© 2026 LetsGit.IT. All rights reserved.

LetsGit.IT/Categories/Data Structures
Data Structureseasy

What is a deque (double-ended queue)?

Tags
#deque#queue#ring-buffer
Back to categoryPractice quiz

Answer

A deque lets you add and remove elements from both the front and the back. With a ring buffer or linked list, these operations are O(1).

Advanced answer

Deep dive

A deque (double-ended queue) supports O(1) insertion/removal at both ends: addFirst/addLast and removeFirst/removeLast. In practice it is often array-backed using a circular buffer (great cache locality) or implemented as a linked list.

A deque can act as:

  • Queue (FIFO): addLast + removeFirst
  • Stack (LIFO): addLast + removeLast

Examples

ArrayDeque<Integer> d = new ArrayDeque<>();
d.addLast(1);
d.addLast(2);
int first = d.removeFirst(); // 1

d.addFirst(10);
int last = d.removeLast(); // 2

Where it shows up

  • BFS: push neighbors to the back, pop from the front.
  • Sliding window / monotonic queue patterns (e.g., max in window).
  • 0-1 BFS: push weight-0 edges to front and weight-1 edges to back.

Common pitfalls

  • Deque is not for random access; removing from the middle is O(n)

Related questions

Data Structures
What is a deque and when would you use it instead of a queue or stack?
#deque#queue#stack
Data Structures
What is a ring buffer (circular queue) and why is it useful?
#ring-buffer#queue#producer-consumer
Data Structures
Stack vs Queue?
#stack#queue
.
  • Choosing a linked-list deque when an array deque is faster for most workloads.
  • Forgetting capacity/resizing behavior (array-backed implementations grow).
  • #data-structure
    Algorithms
    What is a monotonic queue and how does it solve sliding window max in O(n)?
    #deque#monotonic-queue#sliding-window
    Monoliths
    How do you run background jobs in a monolith reliably?
    #jobs#queue#worker