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 after pushing elements on the stack?
Consider the following C# code that pushes elements onto a stack. What will be the printed output?
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); foreach (int item in stack) { Console.Write(item + " -> "); } Console.WriteLine("null"); } }
Attempts:
2 left
💡 Hint
Remember that Stack is Last-In-First-Out (LIFO). The last pushed element is on top.
✗ Incorrect
The stack pushes 10, then 20, then 30. The top is 30, so iterating prints 30 first, then 20, then 10.
❓ Predict Output
intermediateWhat is the stack content after pushing and popping?
What will be the output after executing the following code?
DSA C#
using System; using System.Collections.Generic; class Program { static void Main() { Stack<string> stack = new Stack<string>(); stack.Push("apple"); stack.Push("banana"); stack.Push("cherry"); stack.Pop(); foreach (string fruit in stack) { Console.Write(fruit + " -> "); } Console.WriteLine("null"); } }
Attempts:
2 left
💡 Hint
Pop removes the top element. Which element was last pushed?
✗ Incorrect
After pushing apple, banana, cherry, the stack top is cherry. Pop removes cherry. Remaining stack is banana (top), then apple.
❓ Predict Output
advancedWhat error occurs when popping from an empty stack?
What error will this code produce when executed?
DSA C#
using System; using System.Collections.Generic; class Program { static void Main() { Stack<int> stack = new Stack<int>(); stack.Pop(); } }
Attempts:
2 left
💡 Hint
Popping from empty stack is invalid in C#.
✗ Incorrect
Calling Pop on an empty Stack throws InvalidOperationException in C#.
❓ Predict Output
advancedWhat is the output after pushing elements and converting to array?
What will be printed by this code?
DSA C#
using System; using System.Collections.Generic; class Program { static void Main() { Stack<char> stack = new Stack<char>(); stack.Push('a'); stack.Push('b'); stack.Push('c'); char[] arr = stack.ToArray(); foreach (char ch in arr) { Console.Write(ch + " "); } } }
Attempts:
2 left
💡 Hint
Stack.ToArray returns elements from top to bottom.
✗ Incorrect
Stack has 'c' on top, then 'b', then 'a'. ToArray returns array starting from top element.
🧠 Conceptual
expertWhat is the time complexity of pushing n elements on a stack?
If you push n elements one by one onto an empty stack, what is the overall time complexity?
Attempts:
2 left
💡 Hint
Each push is constant time. Consider total time for n pushes.
✗ Incorrect
Each push operation takes O(1) time. Doing n pushes takes O(n) time overall.
