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 push and pop sequence?
Consider a stack implemented using an array with a maximum size of 3. The following operations are performed in order:
Push(10), Push(20), Pop(), Push(30), Push(40), Pop(), Pop()
What is the printed output of the popped elements?
Push(10), Push(20), Pop(), Push(30), Push(40), Pop(), Pop()
What is the printed output of the popped elements?
DSA C#
class Stack { private int[] arr = new int[3]; private int top = -1; public void Push(int x) { if (top == arr.Length - 1) return; arr[++top] = x; } public int Pop() { if (top == -1) return -1; return arr[top--]; } } var stack = new Stack(); stack.Push(10); stack.Push(20); Console.WriteLine(stack.Pop()); stack.Push(30); stack.Push(40); Console.WriteLine(stack.Pop()); Console.WriteLine(stack.Pop());
Attempts:
2 left
💡 Hint
Remember that pop removes the top element and push adds on top if space is available.
✗ Incorrect
The stack operations proceed as follows: Push 10 and 20, stack is [10,20]. Pop removes 20. Push 30 and 40, stack is [10,30,40]. Pop removes 40, next pop removes 30. So outputs are 20, 40, 30.
❓ Predict Output
intermediateWhat is the stack size after these operations?
Given a stack implemented using an array with max size 5, the following operations are done:
Push(5), Push(10), Pop(), Push(15), Push(20), Pop(), Push(25)
What is the size of the stack after these operations?
Push(5), Push(10), Pop(), Push(15), Push(20), Pop(), Push(25)
What is the size of the stack after these operations?
DSA C#
class Stack { private int[] arr = new int[5]; private int top = -1; public void Push(int x) { if (top == arr.Length - 1) return; arr[++top] = x; } public void Pop() { if (top == -1) return; top--; } public int Size() { return top + 1; } } var stack = new Stack(); stack.Push(5); stack.Push(10); stack.Pop(); stack.Push(15); stack.Push(20); stack.Pop(); stack.Push(25); Console.WriteLine(stack.Size());
Attempts:
2 left
💡 Hint
Count pushes and pops carefully to find current stack size.
✗ Incorrect
Operations: Push 5 (size 1), Push 10 (2), Pop (1), Push 15 (2), Push 20 (3), Pop (2), Push 25 (3). Final size is 3.
🔧 Debug
advancedIdentify the error in this stack pop method
This pop method is part of a stack implemented using an array. What error will occur when calling Pop on an empty stack?
DSA C#
public int Pop() { if (top == 0) { return -1; } return arr[top--]; }
Attempts:
2 left
💡 Hint
Check the condition that detects empty stack carefully.
✗ Incorrect
The condition 'if (top == 0)' wrongly treats top=0 as empty, but top starts at -1 when empty. So when top is -1, the code tries to access arr[-1], causing IndexOutOfRangeException.
🧠 Conceptual
advancedWhat happens when pushing onto a full stack array?
If a stack is implemented using an array of fixed size 4, and the stack currently holds 4 elements, what happens when you try to push another element?
Attempts:
2 left
💡 Hint
Fixed size arrays cannot grow automatically.
✗ Incorrect
In a fixed size array stack, pushing when full does nothing or rejects the push. It does not replace elements or resize automatically. Exception throwing depends on implementation but usually push is ignored.
🚀 Application
expertTrace the stack content after a complex sequence
Given a stack with max size 5, initially empty, perform these operations:
Push(1), Push(2), Push(3), Pop(), Push(4), Pop(), Pop(), Push(5), Push(6)
What is the final stack content from bottom to top?
Push(1), Push(2), Push(3), Pop(), Push(4), Pop(), Pop(), Push(5), Push(6)
What is the final stack content from bottom to top?
DSA C#
class Stack { private int[] arr = new int[5]; private int top = -1; public void Push(int x) { if (top == arr.Length - 1) return; arr[++top] = x; } public int Pop() { if (top == -1) return -1; return arr[top--]; } public string PrintStack() { if (top == -1) return "Empty"; var result = new System.Text.StringBuilder(); for (int i = 0; i <= top; i++) { result.Append(arr[i]); if (i != top) result.Append(" -> "); } return result.ToString(); } } var stack = new Stack(); stack.Push(1); stack.Push(2); stack.Push(3); stack.Pop(); stack.Push(4); stack.Pop(); stack.Pop(); stack.Push(5); stack.Push(6); Console.WriteLine(stack.PrintStack());
Attempts:
2 left
💡 Hint
Track each push and pop carefully, updating the stack content step by step.
✗ Incorrect
Operations: Push 1,2,3 stack=[1,2,3]. Pop removes 3 stack=[1,2]. Push 4 stack=[1,2,4]. Pop removes 4 stack=[1,2]. Pop removes 2 stack=[1]. Push 5 stack=[1,5]. Push 6 stack=[1,5,6]. Final stack is 1 -> 5 -> 6.
