Challenge - 5 Problems
Deque Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Dequeue Operations on a Double-Ended Queue
What is the printed state of the deque after performing the following operations in C#?
Start with an empty deque.
Enqueue 10 at the rear.
Enqueue 20 at the rear.
Enqueue 5 at the front.
Dequeue from the rear.
Dequeue from the front.
Print the deque.
Start with an empty deque.
Enqueue 10 at the rear.
Enqueue 20 at the rear.
Enqueue 5 at the front.
Dequeue from the rear.
Dequeue from the front.
Print the deque.
DSA C#
using System; using System.Collections.Generic; class Program { static void Main() { LinkedList<int> deque = new LinkedList<int>(); deque.AddLast(10); // rear deque.AddLast(20); // rear deque.AddFirst(5); // front deque.RemoveLast(); // dequeue rear deque.RemoveFirst(); // dequeue front foreach (var item in deque) { Console.Write(item + " -> "); } Console.WriteLine("null"); } }
Attempts:
2 left
💡 Hint
Remember that AddFirst adds to front and AddLast adds to rear. RemoveFirst removes from front and RemoveLast removes from rear.
✗ Incorrect
Initially, deque is empty. After AddLast(10) and AddLast(20), deque is 10 -> 20. AddFirst(5) makes it 5 -> 10 -> 20. RemoveLast() removes 20, so deque is 5 -> 10. RemoveFirst() removes 5, leaving 10 only. So the output is '10 -> null'.
❓ Predict Output
intermediateResulting Deque After Mixed Operations
Given the following C# code, what is the final printed deque state?
Start empty.
Enqueue 1 at front.
Enqueue 2 at rear.
Enqueue 3 at front.
Dequeue from rear.
Print the deque.
Start empty.
Enqueue 1 at front.
Enqueue 2 at rear.
Enqueue 3 at front.
Dequeue from rear.
Print the deque.
DSA C#
using System; using System.Collections.Generic; class Program { static void Main() { LinkedList<int> deque = new LinkedList<int>(); deque.AddFirst(1); deque.AddLast(2); deque.AddFirst(3); deque.RemoveLast(); foreach (var item in deque) { Console.Write(item + " -> "); } Console.WriteLine("null"); } }
Attempts:
2 left
💡 Hint
AddFirst adds to front, AddLast adds to rear, RemoveLast removes from rear.
✗ Incorrect
After AddFirst(1), deque is 1. AddLast(2) makes it 1 -> 2. AddFirst(3) makes it 3 -> 1 -> 2. RemoveLast() removes 2, leaving 3 -> 1. So output is '3 -> 1 -> null'.
🔧 Debug
advancedIdentify the Error in Dequeue Operation Code
What error will this C# code produce when run?
Code:
LinkedList deque = new LinkedList();
deque.AddLast(1);
deque.RemoveFirst();
deque.RemoveFirst();
Code:
LinkedList
deque.AddLast(1);
deque.RemoveFirst();
deque.RemoveFirst();
DSA C#
using System; using System.Collections.Generic; class Program { static void Main() { LinkedList<int> deque = new LinkedList<int>(); deque.AddLast(1); deque.RemoveFirst(); deque.RemoveFirst(); } }
Attempts:
2 left
💡 Hint
Removing from an empty LinkedList causes an exception.
✗ Incorrect
After adding 1, RemoveFirst() removes it, deque becomes empty. The next RemoveFirst() tries to remove from empty deque, causing InvalidOperationException.
❓ Predict Output
advancedDeque State After Multiple Front and Rear Operations
What is the output of the following C# code?
Start empty.
AddLast(4), AddFirst(3), AddLast(5), RemoveFirst(), AddFirst(2), RemoveLast(), Print deque.
Start empty.
AddLast(4), AddFirst(3), AddLast(5), RemoveFirst(), AddFirst(2), RemoveLast(), Print deque.
DSA C#
using System; using System.Collections.Generic; class Program { static void Main() { LinkedList<int> deque = new LinkedList<int>(); deque.AddLast(4); // 4 deque.AddFirst(3); // 3 -> 4 deque.AddLast(5); // 3 -> 4 -> 5 deque.RemoveFirst(); // removes 3, deque: 4 -> 5 deque.AddFirst(2); // 2 -> 4 -> 5 deque.RemoveLast(); // removes 5, deque: 2 -> 4 foreach (var item in deque) { Console.Write(item + " -> "); } Console.WriteLine("null"); } }
Attempts:
2 left
💡 Hint
Track each operation carefully, front and rear add/remove.
✗ Incorrect
After all operations, deque contains 2 at front and 4 at rear, so output is '2 -> 4 -> null'.
🧠 Conceptual
expertNumber of Items in Deque After Sequence of Operations
A deque starts empty. The following operations are performed:
1. AddFirst(10)
2. AddLast(20)
3. RemoveFirst()
4. AddLast(30)
5. RemoveLast()
6. AddFirst(40)
How many items are in the deque at the end?
1. AddFirst(10)
2. AddLast(20)
3. RemoveFirst()
4. AddLast(30)
5. RemoveLast()
6. AddFirst(40)
How many items are in the deque at the end?
Attempts:
2 left
💡 Hint
Count items after each operation carefully.
✗ Incorrect
Step by step:
Empty
AddFirst(10): 10
AddLast(20): 10, 20
RemoveFirst(): removes 10, left 20
AddLast(30): 20, 30
RemoveLast(): removes 30, left 20
AddFirst(40): 40, 20
So 2 items remain.
Empty
AddFirst(10): 10
AddLast(20): 10, 20
RemoveFirst(): removes 10, left 20
AddLast(30): 20, 30
RemoveLast(): removes 30, left 20
AddFirst(40): 40, 20
So 2 items remain.
