JavaScript Notes
Learn the Deque (Double-Ended Queue) data structure in JavaScript. Add and remove from both ends, implement with arrays and classes, explore sliding window use cases, and answer interview questions.
A Deque (pronounced *"deck"*), short for Double-Ended Queue, is a data structure that allows you to add and remove elements from both ends — the front and the rear — in O(1) time. It's the most versatile linear data structure, combining the behaviours of both a stack and a queue.
💡 Key insight: A deque is a generalisation — a stack is a deque where you only use one end, and a queue is a deque where you add to one end and remove from the other.
Deque vs Stack vs Queue
Deque Operations
| Method | Description | Time |
|---|---|---|
addFront(val) | Add element to the front | O(1) |
addRear(val) | Add element to the rear | O(1) |
removeFront() | Remove from the front | O(1) |
removeRear() | Remove from the rear | O(1) |
peekFront() | Read front without removing | O(1) |
peekRear() | Read rear without removing | O(1) |
isEmpty() | Check if empty | O(1) |
size() | Number of elements | O(1) |
Deque Class Implementation
"Deque: [A, B, C, D]" "Front: A" "Rear: D" "Remove front: A" "Remove rear: D" "Deque now: [B, C]" "Size: 2"
Using a Deque as a Stack
const stackDeque = new Deque();
stackDeque.addRear(1);
stackDeque.addRear(2);
stackDeque.addRear(3);
// Pop from rear (LIFO)
console.log(stackDeque.removeRear()); // 3
console.log(stackDeque.removeRear()); // 23 2
Using a Deque as a Queue
const queueDeque = new Deque();
queueDeque.addRear("first");
queueDeque.addRear("second");
queueDeque.addRear("third");
// Dequeue from front (FIFO)
console.log(queueDeque.removeFront()); // "first"
console.log(queueDeque.removeFront()); // "second""first" "second"
Real-World Application 1: Palindrome Check
function isPalindrome(str) {
const deque = new Deque();
for (const char of str.toLowerCase().replace(/\s/g, "")) {
deque.addRear(char);
}
while (deque.size() > 1) {
if (deque.removeFront() !== deque.removeRear()) return false;
}
return true;
}
console.log(isPalindrome("racecar")); // true
console.log(isPalindrome("A man a plan a canal Panama")); // true
console.log(isPalindrome("hello")); // falsetrue true false
Real-World Application 2: Sliding Window Maximum
The classic use case for a deque — finding the maximum in each sliding window of size k:
[3, 3, 5, 5, 6, 7]
When to Use a Deque
✅ Use a deque when:
- You need both stack and queue behaviour in one structure
- Sliding window algorithms (max/min in a window)
- Palindrome checking — compare from both ends simultaneously
- Browser history with forward/back navigation
- Undo/redo with history limit — add new actions to rear, drop oldest from front when limit reached
- Scheduling — add urgent tasks to front, regular tasks to rear
❌ Prefer a stack or queue when:
- You only need operations on one end → simpler to use a dedicated stack/queue
Common Mistakes
- Confusing deque with dequeue — "dequeue" is sometimes used as a verb (to remove from a queue). "Deque" is the data structure.
- Using a plain array with
unshift—unshiftis O(n). Use an object-based or doubly-linked implementation for O(1) front operations. - Not checking
isEmptybefore removing — bothremoveFrontandremoveRearthrow on an empty deque.
Interview Questions
Q1. What is a deque and how does it differ from a queue?
A deque (Double-Ended Queue) allows insertion and deletion from both the front and the rear. A regular queue only allows insertion at the rear and deletion from the front. A deque is more flexible — it can be used as either a stack or a queue.
Q2. What are the time complexities of deque operations?
All core operations (addFront,addRear,removeFront,removeRear,peekFront,peekRear) are O(1) with an object-based or doubly-linked list implementation.
Q3. How is a deque used in the Sliding Window Maximum problem?
A monotonic deque stores indices in decreasing order of their values. For each new element, remove all rear elements smaller than the new one (they can never be a window max). Remove any front element that's outside the current window. The front always holds the index of the current window's maximum.
Q4. Can a deque be used as both a stack and a queue?
Yes. Use only the rear end (addRear + removeRear) for LIFO stack behaviour. Use rear for adding and front for removing (addRear + removeFront) for FIFO queue behaviour.
Q5. What is a real-world use case for a deque?
A browser's "recent history with a size limit": add new pages to the rear, remove oldest pages from the front when the limit is exceeded. Users can navigate forward (rear) or backward (front) at any time.
Q6. How does a deque differ from a doubly linked list?
A deque is an abstract data type with a defined interface (add/remove both ends). A doubly linked list is a concrete data structure that can *implement* a deque. A deque can also be implemented with a circular buffer or an array.
Key Takeaways
- A deque adds and removes from both ends — it generalises both stacks and queues.
- All four core operations are O(1) with an efficient implementation (object-based or doubly linked list).
- Classic use cases: palindrome checking, sliding window max/min, undo/redo with history limit, browser navigation.
- Avoid
Array.unshift()for front insertion in performance-critical code — it's O(n).
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Deque (Double-Ended Queue) in JavaScript — Complete Guide.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this JavaScript Master Course topic.
Search Terms
javascript-complete-guide, javascript master course, javascript, complete, guide, data, structures, deque
Related JavaScript Master Course Topics