📖 Scenario: Imagine you are managing a line of customers waiting for service. Sometimes, customers leave from the front or the back of the line. You want to keep track of the line and remove customers from either end as needed.
🎯 Goal: You will create a double-ended queue (deque) using an array, then remove customers from the front and back, and finally display the remaining customers in the line.
📋 What You'll Learn
Create an array called deque with exact string elements representing customers in order
Create two variables called frontRemove and backRemove to store the removed customers
Remove one customer from the front of the deque and store it in frontRemove
Remove one customer from the back of the deque and store it in backRemove
Print the removed customers and the remaining deque in order
💡 Why This Matters
🌍 Real World
Managing lines or queues where people or items can enter or leave from both ends, like customer service lines or task scheduling.
💼 Career
Understanding deque operations is useful for software development roles involving data structure manipulation, real-time systems, and performance optimization.
Progress0 / 4 steps
1
Create the initial deque
Create an array called deque with these exact string elements in order: 'Alice', 'Bob', 'Charlie', 'Diana', 'Ethan'
DSA Typescript
Hint
Use const deque: string[] = [...] to create the array with the exact names.
2
Create variables to store removed customers
Create two variables called frontRemove and backRemove and initialize them with empty strings ''
DSA Typescript
Hint
Use let frontRemove: string = ''; and let backRemove: string = ''; to create the variables.
3
Remove customers from front and back
Remove one customer from the front of deque using shift() and assign it to frontRemove. Remove one customer from the back of deque using pop() and assign it to backRemove.
DSA Typescript
Hint
Use deque.shift() to remove from front and deque.pop() to remove from back.
4
Print removed customers and remaining deque
Print the removed customers using console.log in this exact format: Removed from front: Alice and Removed from back: Ethan. Then print the remaining deque in order using console.log with this exact format: Remaining deque: Bob, Charlie, Diana
DSA Typescript
Hint
Use console.log with template strings and deque.join(', ') to print the results.
Practice
(1/5)
1. What is the main difference between a dequeue and a queue?
easy
A. A dequeue allows insertion and deletion from both ends.
B. A dequeue only allows insertion at the rear.
C. A dequeue only allows deletion from the front.
D. A dequeue is a fixed size data structure.
Solution
Step 1: Understand queue operations
A queue allows insertion at the rear and deletion from the front only.
Step 2: Understand dequeue operations
A dequeue allows insertion and deletion at both front and rear ends.
Final Answer:
A dequeue allows insertion and deletion from both ends. -> Option A
Quick Check:
Dequeue = Both ends operations [OK]
Hint: Dequeue = Double-ended queue, both ends allowed [OK]
Common Mistakes:
Thinking dequeue is same as queue
Assuming insertion only at rear
Confusing dequeue with stack
2. Which of the following is the correct TypeScript method to add an element at the front of a dequeue implemented as an array?
easy
A. dequeue.pushFront(element);
B. dequeue.unshift(element);
C. dequeue.addFirst(element);
D. dequeue.insertFront(element);
Solution
Step 1: Recall array methods for front insertion
In TypeScript/JavaScript, unshift() adds element at the front of an array.
Step 2: Check other options
Methods like pushFront, addFirst, insertFront do not exist in standard arrays.
Final Answer:
dequeue.unshift(element); -> Option B
Quick Check:
Array front insert = unshift() [OK]
Hint: Use unshift() to add at front in arrays [OK]
Common Mistakes:
Using push() instead of unshift()
Assuming pushFront() exists
Confusing with linked list methods
3. Consider this TypeScript code using an array as a dequeue:
let dequeue = [10, 20, 30];
dequeue.pop();
dequeue.unshift(5);
console.log(dequeue);
What is the output?
medium
A. [5, 10, 20]
B. [5, 10, 20, 30]
C. [10, 20, 30, 5]
D. [10, 20]
Solution
Step 1: Execute pop()
pop() removes last element 30, so array becomes [10, 20].
Step 2: Execute unshift(5)
unshift(5) adds 5 at front, array becomes [5, 10, 20].
Final Answer:
[5, 10, 20] -> Option A
Quick Check:
pop removes last, unshift adds front [OK]
Hint: pop removes last, unshift adds front [OK]
Common Mistakes:
Assuming pop removes front
Thinking unshift adds at end
Confusing array order after operations
4. The following TypeScript code tries to remove an element from the front of a dequeue implemented as an array:
let dequeue = [1, 2, 3];
dequeue.removeFront();
console.log(dequeue);
What is the issue and how to fix it?
medium
A. removeFront() works only on linked lists; convert array to linked list.
B. removeFront() removes last element; use pop() instead.
C. removeFront() is not a valid method; use shift() instead.
D. No issue; code runs correctly.
Solution
Step 1: Identify invalid method
Arrays in TypeScript do not have removeFront() method; this causes a runtime error.
Step 2: Use correct method for front removal
shift() removes the first element from an array correctly.
Final Answer:
removeFront() is not a valid method; use shift() instead. -> Option C
Quick Check:
Front removal in arrays = shift() [OK]
Hint: Use shift() to remove front element in arrays [OK]
Common Mistakes:
Using non-existent removeFront() method
Confusing pop() with front removal
Thinking arrays have linked list methods
5. You have a dequeue implemented as an array: let dq = [1, 2, 3, 4];. You perform these operations: