Challenge - 5 Problems
Stack Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Direct Array Use as Stack
What is the printed output after running this C# code that uses an array directly as a stack?
DSA C#
int[] stack = new int[3]; int top = -1; // Push elements stack[++top] = 10; stack[++top] = 20; stack[++top] = 30; // Pop one element int popped = stack[top--]; // Print remaining stack elements for (int i = 0; i <= top; i++) { Console.Write(stack[i] + " -> "); } Console.WriteLine("null");
Attempts:
2 left
💡 Hint
Remember that popping decreases the top index, so the last pushed element is removed.
✗ Incorrect
The code pushes 10, 20, 30 onto the array, then pops once, removing 30. The remaining elements are 10 and 20, printed in order.
🧠 Conceptual
intermediateWhy Use Stack Abstraction Instead of Direct Array?
Which of the following is the main reason to use a stack abstraction instead of directly using an array for stack operations?
Attempts:
2 left
💡 Hint
Think about safety and error prevention when using a stack.
✗ Incorrect
Stack abstraction controls push/pop operations and prevents accessing invalid positions, which direct array use does not protect against.
❓ Predict Output
advancedStack Abstraction Behavior on Overflow
What will be the output of this C# code using a Stack class abstraction when pushing more elements than capacity?
DSA C#
class Stack { private int[] arr; private int top = -1; private int capacity; public Stack(int size) { capacity = size; arr = new int[capacity]; } public void Push(int x) { if (top == capacity - 1) { Console.WriteLine("Stack Overflow"); return; } arr[++top] = x; } public void Print() { for (int i = 0; i <= top; i++) { Console.Write(arr[i] + " -> "); } Console.WriteLine("null"); } } var stack = new Stack(2); stack.Push(5); stack.Push(10); stack.Push(15); stack.Print();
Attempts:
2 left
💡 Hint
Check the condition that prevents pushing when the stack is full.
✗ Incorrect
The third push triggers "Stack Overflow" message and does not add 15. The stack contains only 5 and 10.
🔧 Debug
advancedIdentify the Bug in Direct Array Stack Pop
What error will this C# code produce when popping from an empty stack implemented with an array and top index?
DSA C#
int[] stack = new int[2]; int top = -1; // Pop without push int popped = stack[top--]; Console.WriteLine(popped);
Attempts:
2 left
💡 Hint
Consider what happens when you access an array with a negative index.
✗ Incorrect
Accessing stack[-1] causes IndexOutOfRangeException because array indices cannot be negative.
🚀 Application
expertStack Abstraction Benefits in Real-World Scenario
In a program managing browser history using a stack abstraction, which benefit is NOT provided by using stack abstraction over direct array use?
Attempts:
2 left
💡 Hint
Think about what stack abstraction controls and what it does not allow.
✗ Incorrect
Stack abstraction does not allow random access by index; it only allows push/pop at the top. Direct array use might allow index access but loses safety.
