Challenge - 5 Problems
Stack Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this stack operation sequence?
Consider a stack where we push integers 1, 2, 3 in order, then pop one element, then push 4. What is the printed stack from top to bottom?
DSA C#
Stack<int> stack = new Stack<int>(); stack.Push(1); stack.Push(2); stack.Push(3); stack.Pop(); stack.Push(4); while(stack.Count > 0) { Console.Write(stack.Pop()); if(stack.Count > 0) Console.Write(" -> "); } Console.WriteLine(" -> null");
Attempts:
2 left
💡 Hint
Remember that stack follows Last In First Out (LIFO). The last pushed element is popped first.
✗ Incorrect
We push 1, 2, 3. Stack top is 3. Then pop removes 3. Stack top is 2. Then push 4. Stack top is 4. So popping all prints 4, 2, 1.
🧠 Conceptual
intermediateWhich statement best describes the LIFO principle in stacks?
Choose the statement that correctly explains the Last In First Out (LIFO) principle used by stacks.
Attempts:
2 left
💡 Hint
Think about which element is on top of the stack.
✗ Incorrect
LIFO means the last element pushed onto the stack is the first one popped off.
🔧 Debug
advancedWhat error occurs when popping from an empty stack?
Given this code snippet, what error will be raised when the Pop method is called on an empty stack?
DSA C#
Stack<int> stack = new Stack<int>(); stack.Pop();
Attempts:
2 left
💡 Hint
Check what exception Stack.Pop throws if empty.
✗ Incorrect
Calling Pop on an empty stack throws InvalidOperationException in C#.
🚀 Application
advancedWhat is the stack content after this sequence of operations?
Start with an empty stack. Perform these operations in order: Push(10), Push(20), Pop(), Push(30), Push(40), Pop(), Push(50). What is the stack content from top to bottom?
Attempts:
2 left
💡 Hint
Track each push and pop carefully, remembering LIFO.
✗ Incorrect
Push 10, stack: 10
Push 20, stack: 20,10
Pop removes 20, stack: 10
Push 30, stack: 30,10
Push 40, stack: 40,30,10
Pop removes 40, stack: 30,10
Push 50, stack: 50,30,10
❓ Predict Output
expertWhat is the output of this C# stack iteration code?
Given this code, what is printed to the console?
DSA C#
Stack<string> stack = new Stack<string>(); stack.Push("apple"); stack.Push("banana"); stack.Push("cherry"); foreach(var item in stack) { Console.Write(item + " -> "); } Console.WriteLine("null");
Attempts:
2 left
💡 Hint
Stack enumeration goes from top to bottom without removing elements.
✗ Incorrect
Stack enumerates from top to bottom, so order is cherry, banana, apple.
