A dequeue (double-ended queue) is a data structure where elements can be added or removed from both the front and the rear ends.
Click to reveal answer
beginner
Name the two main operations for removing elements in a dequeue.
The two main removal operations are dequeueFront (removes from the front) and dequeueRear (removes from the rear).
Click to reveal answer
beginner
What happens if you try to dequeue from an empty dequeue?
If you try to dequeue from an empty dequeue, it results in an underflow condition, meaning there are no elements to remove.
Click to reveal answer
intermediate
In a dequeue implemented with a circular array, why do we use modulo (%) operation?
Modulo operation helps wrap around the indices when they reach the end of the array, allowing the dequeue to use the array space efficiently in a circular manner.
Click to reveal answer
intermediate
Explain the difference between dequeueFront and dequeueRear operations.
dequeueFront removes the element at the front end and moves the front pointer forward, while dequeueRear removes the element at the rear end and moves the rear pointer backward.
Click to reveal answer
Which operation removes an element from the rear end of a dequeue?
AenqueueRear
BdequeueFront
CdequeueRear
DenqueueFront
✗ Incorrect
dequeueRear removes an element from the rear end of the dequeue.
What condition occurs when trying to dequeue from an empty dequeue?
AUnderflow
BOverflow
CSegmentation fault
DNo operation
✗ Incorrect
Underflow happens when removing from an empty dequeue.
In a circular array dequeue, what does the modulo operator (%) help with?
AWrapping indices around the array
BSorting elements
CIncreasing array size
DChecking if dequeue is empty
✗ Incorrect
Modulo helps wrap indices to the start when they reach the array end.
Which pointer moves forward when performing dequeueFront?
ARear pointer
BFront pointer
CBoth pointers
DNo pointer moves
✗ Incorrect
The front pointer moves forward after removing the front element.
Which operation adds an element to the front of a dequeue?
AdequeueRear
BdequeueFront
CenqueueRear
DenqueueFront
✗ Incorrect
enqueueFront adds an element at the front end.
Describe the steps involved in the dequeueFront operation on a dequeue implemented with a circular array.
Think about how the front pointer moves and how to handle empty dequeue.
You got /4 concepts.
Explain how dequeueRear operation differs from dequeueFront in a dequeue.
Focus on which end is affected and pointer movement.
You got /4 concepts.
Practice
(1/5)
1. What is the main difference between a deque and a regular queue in data structures?
easy
A. Deque allows insertion and deletion from both ends, queue only from front and rear respectively
B. Deque only allows insertion at the rear, queue allows insertion at both ends
C. Deque is always sorted, queue is not
D. Deque uses arrays, queue uses linked lists
Solution
Step 1: Understand queue operations
A regular queue allows insertion at the rear and deletion from the front only.
Step 2: Understand deque operations
A deque (double-ended queue) allows insertion and deletion at both front and rear ends.
Final Answer:
Deque allows insertion and deletion from both ends, queue only from front and rear respectively -> Option A
Quick Check:
Deque = double-ended queue [OK]
Hint: Deque means double-ended, so both ends allowed [OK]
Common Mistakes:
Thinking deque only inserts at rear
Confusing deque with sorted data structure
Assuming deque uses only arrays
2. Which of the following is the correct syntax to insert an element 10 at the front of a std::deque<int> named d in C++?
easy
A. d.push(10);
B. d.push_front(10);
C. d.insert_front(10);
D. d.add_front(10);
Solution
Step 1: Recall deque insertion methods
In C++, std::deque uses push_front() to insert at the front.
Step 2: Check method names
Only push_front(10); is valid syntax for front insertion.
Final Answer:
d.push_front(10); -> Option B
Quick Check:
push_front() inserts at front [OK]
Hint: Use push_front() to add at front in deque [OK]
Common Mistakes:
Using push() which is not a deque method
Using insert_front() which doesn't exist
Confusing with vector methods
3. What will be the output of the following C++ code snippet?
std::deque<int> d = {1, 2, 3};
d.push_front(0);
d.push_back(4);
d.pop_front();
for (int x : d) std::cout << x << " ";
medium
A. 1 2 3 4
B. 0 1 2 3 4
C. 1 2 3
D. 0 1 2 3
Solution
Step 1: Trace deque operations
Initial deque: [1, 2, 3] After push_front(0): [0, 1, 2, 3] After push_back(4): [0, 1, 2, 3, 4] After pop_front(): removes 0, deque becomes [1, 2, 3, 4]
Step 2: Print elements
Elements printed are 1 2 3 4 with spaces.
Final Answer:
1 2 3 4 -> Option A
Quick Check:
pop_front removes first element [OK]
Hint: pop_front removes first inserted element [OK]
Common Mistakes:
Forgetting pop_front removes 0
Confusing push_front and push_back order
Printing without spaces
4. Identify the error in the following C++ code using std::deque: