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 check code?
Consider a stack with a maximum size of 3. After pushing two elements, what will be the output of the empty and full checks?
DSA C#
using System; class Stack { private int[] items; private int top; private int maxSize; public Stack(int size) { maxSize = size; items = new int[maxSize]; top = -1; } public void Push(int item) { if (!IsFull()) { items[++top] = item; } } public bool IsEmpty() { return top == -1; } public bool IsFull() { return top == maxSize - 1; } } class Program { static void Main() { Stack stack = new Stack(3); stack.Push(10); stack.Push(20); Console.WriteLine(stack.IsEmpty()); Console.WriteLine(stack.IsFull()); } }
Attempts:
2 left
💡 Hint
Think about how many elements are pushed and the stack's max size.
✗ Incorrect
The stack has size 3 and two elements pushed. So it is not empty (IsEmpty() returns false) and not full (IsFull() returns false).
❓ Predict Output
intermediateWhat happens when pushing to a full stack?
Given a stack of size 2, after pushing two elements, what will be the output of the full check and what happens if we push one more element?
DSA C#
using System; class Stack { private int[] items; private int top; private int maxSize; public Stack(int size) { maxSize = size; items = new int[maxSize]; top = -1; } public void Push(int item) { if (!IsFull()) { items[++top] = item; } } public bool IsFull() { return top == maxSize - 1; } public int Top() { if (top >= 0) return items[top]; return -1; } } class Program { static void Main() { Stack stack = new Stack(2); stack.Push(5); stack.Push(15); Console.WriteLine(stack.IsFull()); stack.Push(25); Console.WriteLine(stack.Top()); } }
Attempts:
2 left
💡 Hint
Check if the push method prevents adding when full.
✗ Incorrect
The stack is full after pushing 5 and 15. The third push is ignored. So IsFull() returns true and Top() returns 15.
❓ Predict Output
advancedWhat is the output after popping all elements and checking empty?
Given a stack of size 2, after pushing two elements and popping both, what will be the output of the empty check?
DSA C#
using System; class Stack { private int[] items; private int top; private int maxSize; public Stack(int size) { maxSize = size; items = new int[maxSize]; top = -1; } public void Push(int item) { if (!IsFull()) { items[++top] = item; } } public int Pop() { if (!IsEmpty()) { return items[top--]; } return -1; } public bool IsEmpty() { return top == -1; } } class Program { static void Main() { Stack stack = new Stack(2); stack.Push(100); stack.Push(200); stack.Pop(); stack.Pop(); Console.WriteLine(stack.IsEmpty()); } }
Attempts:
2 left
💡 Hint
After popping all elements, the stack should be empty.
✗ Incorrect
Both elements are popped, so top is -1 and IsEmpty() returns true.
❓ Predict Output
advancedWhat error occurs when popping from an empty stack?
What will be the output or error when popping from an empty stack?
DSA C#
using System; class Stack { private int[] items; private int top; private int maxSize; public Stack(int size) { maxSize = size; items = new int[maxSize]; top = -1; } public int Pop() { if (!IsEmpty()) { return items[top--]; } throw new InvalidOperationException("Stack is empty"); } public bool IsEmpty() { return top == -1; } } class Program { static void Main() { Stack stack = new Stack(1); try { stack.Pop(); } catch (Exception e) { Console.WriteLine(e.GetType().Name); } } }
Attempts:
2 left
💡 Hint
Check the exception thrown when popping empty stack.
✗ Incorrect
The code throws InvalidOperationException with message "Stack is empty" when popping empty stack.
🧠 Conceptual
expertHow many elements can a stack hold before it is full?
If a stack is implemented with an array of size N, how many elements can it hold before IsFull() returns true?
Attempts:
2 left
💡 Hint
Think about array indexing and maximum capacity.
✗ Incorrect
The stack can hold exactly N elements because the array size is N and top index goes from -1 to N-1.
