Introductions
A Deque (Double-Ended Queue) is a linear data structure that allows elements to be added and removed from both the front and rear. These practice questions focus on actually working with a deque: inserting and deleting from both ends, viewing front and rear elements, reversing a deque, processing values from either side, and implementing a deque using JavaScript. The questions gradually move from basic operations to practical problems. Data Structure Deque practice questions with solutions help to build concepts.
Question 1: Insert Elements at the Rear of a Deque
Question
Create an empty deque and insert 10, 20, and 30 at the rear. Display the deque.
Solution
A deque allows insertion from both ends. For this question, we will insert from the rear.
Start with:
let deque = [];
Add the values using push():
deque.push(10);
deque.push(20);
deque.push(30);
The deque becomes:
Front → 10 → 20 → 30 ← Rear
Complete code:
let deque = [];
deque.push(10);
deque.push(20);
deque.push(30);
console.log(deque);
Output
[10, 20, 30]
Answer
The values are inserted at the rear in this order:
Front → 10 → 20 → 30 ← Rear
Question 2: Insert an Element at the Front
Question
Given:
Front → 20 → 30 → 40 ← Rear
Insert 10 at the front.
Solution
In JavaScript, unshift() adds an element to the beginning of an array.
let deque = [20, 30, 40];
deque.unshift(10);
console.log(deque);
Before insertion:
Front → 20 → 30 → 40 ← Rear
After inserting 10:
Front → 10 → 20 → 30 → 40 ← Rear
Output
[10, 20, 30, 40]
Answer
10 becomes the new front element.
Question 3: Remove an Element from the Front
Question
Remove the front element from:
Front → 10 → 20 → 30 → 40 ← Rear
Print the removed value and the remaining deque.
Solution
The front element can be removed using shift().
let deque = [10, 20, 30, 40];
let removed = deque.shift();
console.log("Removed:", removed);
console.log("Deque:", deque);
shift() removes the first element.
Before:
10 → 20 → 30 → 40
After:
20 → 30 → 40
Output
Removed: 10
Deque: [20, 30, 40]
Answer
The front element 10 is removed.
Question 4: Remove an Element from the Rear
Question
Remove the rear element from:
Front → 10 → 20 → 30 → 40 ← Rear
Print the removed value and the remaining deque.
Solution
The rear is the last element of the array.
We can remove it using pop().
let deque = [10, 20, 30, 40];
let removed = deque.pop();
console.log("Removed:", removed);
console.log("Deque:", deque);
Before:
Front → 10 → 20 → 30 → 40 ← Rear
After removing 40:
Front → 10 → 20 → 30 ← Rear
Output
Removed: 40
Deque: [10, 20, 30]
Answer
The rear element 40 is removed.
Question 5: Insert Elements from Both Ends
Question
Start with an empty deque.
Perform these operations in order:
Insert 20 at rear
Insert 30 at rear
Insert 10 at front
Insert 40 at rear
Insert 5 at front
Display the final deque.
Solution
Start:
[]
Insert 20 at rear:
[20]
Insert 30 at rear:
[20, 30]
Insert 10 at front:
[10, 20, 30]
Insert 40 at rear:
[10, 20, 30, 40]
Insert 5 at front:
[5, 10, 20, 30, 40]
Complete code:
let deque = [];
deque.push(20);
deque.push(30);
deque.unshift(10);
deque.push(40);
deque.unshift(5);
console.log(deque);
Output
[5, 10, 20, 30, 40]
Answer
The final deque is:
Front → 5 → 10 → 20 → 30 → 40 ← Rear
This question demonstrates the main advantage of a deque: insertion can happen at either end.
Question 6: Remove Elements from Both Ends
Question
Given:
Front → 10 → 20 → 30 → 40 → 50 ← Rear
Perform these operations:
Remove from front
Remove from rear
Print both removed values and the remaining deque.
Solution
Use shift() for the front and pop() for the rear.
let deque = [10, 20, 30, 40, 50];
let frontRemoved = deque.shift();
let rearRemoved = deque.pop();
console.log("Removed from Front:", frontRemoved);
console.log("Removed from Rear:", rearRemoved);
console.log("Deque:", deque);
First operation:
10 is removed
Deque:
20 → 30 → 40 → 50
Second operation:
50 is removed
Final deque:
20 → 30 → 40
Output
Removed from Front: 10
Removed from Rear: 50
Deque: [20, 30, 40]
Answer
The deque allows deletion from both ends independently.
Question 7: Find the Front and Rear Elements
Question
Find the front and rear elements of:
Front → 15 → 25 → 35 → 45 → 55 ← Rear
without removing any element.
Solution
The front is the first array element:
deque[0]
The rear is the last array element:
deque[deque.length - 1]
Code:
let deque = [15, 25, 35, 45, 55];
let front = deque[0];
let rear = deque[deque.length - 1];
console.log("Front:", front);
console.log("Rear:", rear);
The deque does not change.
Output
Front: 15
Rear: 55
Answer
The front is 15 and the rear is 55.
Question 8: Process a Deque from the Larger End
Question
Given:
[15, 80, 25, 60, 40]
Repeatedly compare the front and rear elements. Remove and print the larger value each time until the deque becomes empty.
Solution
At every step, compare:
Front
and
Rear
If the front is larger, remove it using shift().
Otherwise, remove the rear using pop().
Code:
let deque = [15, 80, 25, 60, 40];
while (deque.length > 0) {
let front = deque[0];
let rear = deque[deque.length - 1];
if (front >= rear) {
console.log("Removed:", deque.shift());
} else {
console.log("Removed:", deque.pop());
}
}
Let’s follow the process:
Front = 15
Rear = 40
Remove 40
Remaining:
[15, 80, 25, 60]
Now:
Front = 15
Rear = 60
Remove 60
Remaining:
[15, 80, 25]
Now:
Front = 15
Rear = 25
Remove 25
Remaining:
[15, 80]
Now:
Front = 15
Rear = 80
Remove 80
Finally:
[15]
Remove 15.
Output
Removed: 40
Removed: 60
Removed: 25
Removed: 80
Removed: 15
Answer
The program always removes the larger value from the two available ends.
Question 9: Reverse a Deque Using Front and Rear
Question
Reverse this deque:
[10, 20, 30, 40, 50]
Expected result:
[50, 40, 30, 20, 10]
Solution
A deque allows us to remove elements from both ends.
We can create a new array and repeatedly remove elements from the rear.
let deque = [10, 20, 30, 40, 50];
let reversed = [];
while (deque.length > 0) {
reversed.push(deque.pop());
}
console.log(reversed);
The process is:
pop() → 50
pop() → 40
pop() → 30
pop() → 20
pop() → 10
So:
reversed = [50, 40, 30, 20, 10]
Output
[50, 40, 30, 20, 10]
Answer
Removing elements from the rear one by one produces the reverse order.
Question 10: Implement a Deque Using a JavaScript Class
Question
Create a Deque class with these operations:
addFront()
addRear()
removeFront()
removeRear()
getFront()
getRear()
Test the class using multiple operations from both ends.
Solution
First create the class:
class Deque {
constructor() {
this.items = [];
}
}
Add an element at the front:
addFront(value) {
this.items.unshift(value);
}
Add an element at the rear:
addRear(value) {
this.items.push(value);
}
Remove from the front:
removeFront() {
return this.items.shift();
}
Remove from the rear:
removeRear() {
return this.items.pop();
}
Get the front:
getFront() {
return this.items[0];
}
Get the rear:
getRear() {
return this.items[this.items.length - 1];
}
Complete code:
class Deque {
constructor() {
this.items = [];
}
addFront(value) {
this.items.unshift(value);
}
addRear(value) {
this.items.push(value);
}
removeFront() {
return this.items.shift();
}
removeRear() {
return this.items.pop();
}
getFront() {
return this.items[0];
}
getRear() {
return this.items[this.items.length - 1];
}
}
let deque = new Deque();
deque.addRear(20);
deque.addRear(30);
deque.addFront(10);
deque.addFront(5);
console.log("Deque:", deque.items);
console.log("Front:", deque.getFront());
console.log("Rear:", deque.getRear());
console.log("Removed Front:", deque.removeFront());
console.log("Removed Rear:", deque.removeRear());
console.log("Deque:", deque.items);
Let’s follow the operations.
Add 20 at rear:
[20]
Add 30 at rear:
[20, 30]
Add 10 at front:
[10, 20, 30]
Add 5 at front:
[5, 10, 20, 30]
Now:
Front → 5 → 10 → 20 → 30 ← Rear
Remove from front:
5 removed
Deque:
[10, 20, 30]
Remove from rear:
30 removed
Final deque:
[10, 20]
Output
Deque: [5, 10, 20, 30]
Front: 5
Rear: 30
Removed Front: 5
Removed Rear: 30
Deque: [10, 20]
Answer
The Deque class supports insertion, deletion, and access from both the front and rear.
Key Takeaways
- A Deque stands for Double-Ended Queue.
- A deque allows insertion from both the front and rear.
- A deque also allows deletion from both the front and rear.
unshift()can be used to insert at the front of a JavaScript array.push()can be used to insert at the rear.shift()removes an element from the front.pop()removes an element from the rear.- The front is the first element and the rear is the last element.
- Unlike a normal queue, a deque does not restrict insertion and deletion to one specific end.
- A deque can be used to process data from either direction.
- Deques are useful in sliding-window problems, scheduling, palindrome checking, caching, and certain graph algorithms.
- The basic deque operations are addFront, addRear, removeFront, and removeRear.
- When implemented with an appropriate deque structure, insertion and deletion at both ends can be performed in O(1) time.
FAQs
1. What is a deque in data structures?
A deque, or double-ended queue, is a linear data structure that allows elements to be inserted and removed from both the front and rear.
2. What does deque stand for?
Deque stands for Double-Ended Queue.
3. How is a deque different from a queue?
A normal queue generally inserts elements at the rear and removes them from the front. A deque allows insertion and removal from both ends.
4. What are the main operations of a deque?
The main operations are adding at the front, adding at the rear, removing from the front, and removing from the rear.
5. Can a deque be used as a stack?
Yes. By restricting operations to one end, a deque can behave like a stack.
6. Can a deque be used as a queue?
Yes. By inserting at one end and removing from the other, a deque can behave like a normal queue.
7. Where are deques used in programming?
Deques are used in sliding-window algorithms, palindrome checking, task processing, caching systems, scheduling, and algorithms that need access to both ends of a sequence.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
