Challenge - 5 Problems
Deque Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Deque Operations Sequence
What is the printed state of the deque after executing the following C# code?
```csharp using System; using System.Collections.Generic; class Program { static void Main() { LinkedList deque = new LinkedList();
deque.AddLast(10);
deque.AddFirst(20);
deque.AddLast(30);
deque.RemoveFirst();
deque.AddFirst(40);
foreach(var item in deque) {
Console.Write(item + " -> ");
}
Console.WriteLine("null");
}
}
```
```csharp using System; using System.Collections.Generic; class Program { static void Main() { LinkedList
DSA C#
using System; using System.Collections.Generic; class Program { static void Main() { LinkedList<int> deque = new LinkedList<int>(); deque.AddLast(10); deque.AddFirst(20); deque.AddLast(30); deque.RemoveFirst(); deque.AddFirst(40); foreach(var item in deque) { Console.Write(item + " -> "); } Console.WriteLine("null"); } }
Attempts:
2 left
💡 Hint
Trace each operation step-by-step, noting how elements are added or removed from the front or back.
✗ Incorrect
Initially, deque is empty. AddLast(10) makes it: 10. AddFirst(20) makes it: 20 -> 10. AddLast(30) makes it: 20 -> 10 -> 30. RemoveFirst() removes 20, so deque is: 10 -> 30. AddFirst(40) makes it: 40 -> 10 -> 30. The printed output matches option C.
🧠 Conceptual
intermediateDeque Size After Mixed Operations
Consider a deque initially empty. We perform these operations in order:
1. AddFirst(5)
2. AddLast(10)
3. AddFirst(15)
4. RemoveLast()
5. RemoveFirst()
How many elements remain in the deque after these operations?
1. AddFirst(5)
2. AddLast(10)
3. AddFirst(15)
4. RemoveLast()
5. RemoveFirst()
How many elements remain in the deque after these operations?
Attempts:
2 left
💡 Hint
Track the deque size after each operation carefully.
✗ Incorrect
Start empty. AddFirst(5): size 1 (5). AddLast(10): size 2 (5,10). AddFirst(15): size 3 (15,5,10). RemoveLast(): removes 10, size 2 (15,5). RemoveFirst(): removes 15, size 1 (5). So 1 element remains.
🔧 Debug
advancedIdentify the Runtime Error in Deque Code
What error will this C# code produce when run?
```csharp using System; using System.Collections.Generic; class Program { static void Main() { LinkedList deque = new LinkedList();
deque.RemoveFirst();
deque.AddLast(1);
Console.WriteLine(deque.First.Value);
}
}
```
```csharp using System; using System.Collections.Generic; class Program { static void Main() { LinkedList
DSA C#
using System; using System.Collections.Generic; class Program { static void Main() { LinkedList<int> deque = new LinkedList<int>(); deque.RemoveFirst(); deque.AddLast(1); Console.WriteLine(deque.First.Value); } }
Attempts:
2 left
💡 Hint
Removing from an empty LinkedList causes an exception.
✗ Incorrect
Calling RemoveFirst() on an empty LinkedList throws InvalidOperationException because there is no element to remove. The code does not reach AddLast(1) or printing.
❓ Predict Output
advancedDeque State After Complex Operations
What is the output of the following C# code?
```csharp using System; using System.Collections.Generic; class Program { static void Main() { LinkedList deque = new LinkedList();
deque.AddLast(1);
deque.AddLast(2);
deque.AddFirst(3);
deque.RemoveLast();
deque.AddFirst(4);
deque.RemoveFirst();
foreach(var item in deque) {
Console.Write(item + " -> ");
}
Console.WriteLine("null");
}
}
```
```csharp using System; using System.Collections.Generic; class Program { static void Main() { LinkedList
DSA C#
using System; using System.Collections.Generic; class Program { static void Main() { LinkedList<int> deque = new LinkedList<int>(); deque.AddLast(1); deque.AddLast(2); deque.AddFirst(3); deque.RemoveLast(); deque.AddFirst(4); deque.RemoveFirst(); foreach(var item in deque) { Console.Write(item + " -> "); } Console.WriteLine("null"); } }
Attempts:
2 left
💡 Hint
Follow each operation carefully, especially the removes and adds at front and back.
✗ Incorrect
Start empty. AddLast(1): 1. AddLast(2): 1 -> 2. AddFirst(3): 3 -> 1 -> 2. RemoveLast(): removes 2, now 3 -> 1. AddFirst(4): 4 -> 3 -> 1. RemoveFirst(): removes 4, now 3 -> 1. Output matches option A.
🧠 Conceptual
expertDeque Behavior Under Concurrent Modifications
In a multi-threaded C# program using a LinkedList as a deque without locks, what is the most likely problem that can occur when two threads simultaneously call AddFirst() and RemoveLast()?
Attempts:
2 left
💡 Hint
Consider thread safety of LinkedList in .NET and what happens without synchronization.
✗ Incorrect
LinkedList is not thread-safe. Concurrent modifications without locks can cause race conditions, corrupt internal pointers, or throw exceptions. It does not synchronize automatically, and deadlocks require locks which are not used here.
