Challenge - 5 Problems
Queue vs Stack Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Stack Operations
What is the printed output after executing the following C# code using a stack?
DSA C#
using System; using System.Collections.Generic; class Program { static void Main() { Stack<int> stack = new Stack<int>(); stack.Push(10); stack.Push(20); stack.Push(30); Console.WriteLine(string.Join(" -> ", stack)); stack.Pop(); stack.Push(40); Console.WriteLine(string.Join(" -> ", stack)); } }
Attempts:
2 left
💡 Hint
Remember that Stack is Last In First Out (LIFO).
✗ Incorrect
The stack prints elements from top to bottom. Initially, 30 is on top, then 20, then 10. After popping 30 and pushing 40, the top is 40, then 20, then 10.
❓ Predict Output
intermediateOutput of Queue Operations
What is the printed output after executing the following C# code using a queue?
DSA C#
using System; using System.Collections.Generic; class Program { static void Main() { Queue<string> queue = new Queue<string>(); queue.Enqueue("A"); queue.Enqueue("B"); queue.Enqueue("C"); Console.WriteLine(string.Join(" -> ", queue)); queue.Dequeue(); queue.Enqueue("D"); Console.WriteLine(string.Join(" -> ", queue)); } }
Attempts:
2 left
💡 Hint
Queue is First In First Out (FIFO).
✗ Incorrect
The queue prints elements from front to back. Initially, A is front, then B, then C. After dequeuing A and enqueueing D, front is B, then C, then D.
🧠 Conceptual
advancedChoosing Between Stack and Queue
Which data structure is best suited for implementing undo functionality in a text editor?
Attempts:
2 left
💡 Hint
Undo means reversing the last action first.
✗ Incorrect
Undo requires reversing the most recent action first, so LIFO behavior of stack fits perfectly.
🧠 Conceptual
advancedWhen to Use Queue Instead of Stack
In which scenario is a queue more appropriate than a stack?
Attempts:
2 left
💡 Hint
Think about order of processing tasks.
✗ Incorrect
Queues process items in the order they arrive (FIFO), ideal for tasks like print jobs.
🔧 Debug
expertIdentify the Error in Stack Usage
What error will occur when running this C# code snippet?
DSA C#
using System; using System.Collections.Generic; class Program { static void Main() { Stack<int> stack = new Stack<int>(); stack.Push(1); stack.Pop(); stack.Pop(); } }
Attempts:
2 left
💡 Hint
Check what happens when popping more times than pushed.
✗ Incorrect
After popping the only element, the stack is empty. Another pop causes InvalidOperationException.
