Challenge - 5 Problems
Min Stack Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Min Stack after Push and Pop
What is the output of the min stack after performing the following operations?
Operations:
push(5), push(3), push(7), pop(), getMin(), pop(), getMin()
Operations:
push(5), push(3), push(7), pop(), getMin(), pop(), getMin()
DSA C#
MinStack stack = new MinStack(); stack.Push(5); stack.Push(3); stack.Push(7); stack.Pop(); int min1 = stack.GetMin(); stack.Pop(); int min2 = stack.GetMin(); Console.WriteLine($"{min1} {min2}");
Attempts:
2 left
💡 Hint
Remember the min stack keeps track of the smallest element at each push.
✗ Incorrect
After pushing 5, 3, 7, the stack is [5,3,7]. Popping removes 7. getMin() returns 3. Popping removes 3. getMin() returns 5.
❓ Predict Output
intermediateValue of Min after Sequence of Operations
After executing these operations on a min stack:
push(2), push(2), push(1), pop(), getMin(), pop(), getMin()
What is the output of the two getMin() calls?
push(2), push(2), push(1), pop(), getMin(), pop(), getMin()
What is the output of the two getMin() calls?
DSA C#
MinStack stack = new MinStack(); stack.Push(2); stack.Push(2); stack.Push(1); stack.Pop(); int min1 = stack.GetMin(); stack.Pop(); int min2 = stack.GetMin(); Console.WriteLine($"{min1} {min2}");
Attempts:
2 left
💡 Hint
Check how duplicates of the minimum value are handled.
✗ Incorrect
After pushing 2,2,1, stack is [2,2,1]. Pop removes 1. getMin() is 2. Pop removes 2. getMin() remains 2.
🔧 Debug
advancedIdentify the Bug in Min Stack Push Method
Given this push method for a min stack, what is the bug causing incorrect min values?
public void Push(int x) { stack.Push(x); if (minStack.Count == 0 || x < minStack.Peek()) { minStack.Push(x); } }
public void Push(int x) { stack.Push(x); if (minStack.Count == 0 || x < minStack.Peek()) { minStack.Push(x); } }
DSA C#
public void Push(int x) { stack.Push(x); if (minStack.Count == 0 || x < minStack.Peek()) { minStack.Push(x); } }
Attempts:
2 left
💡 Hint
Think about what happens when the new value equals the current minimum.
✗ Incorrect
The condition x < minStack.Peek() excludes equal values, so duplicates of min are not pushed. When popping min, minStack loses track of duplicates causing wrong min.
🚀 Application
advancedMinimum Element Tracking in Real-Time Data Stream
You want to design a system that tracks the minimum value in a stream of integers arriving in real-time. Which data structure approach best suits this requirement?
Attempts:
2 left
💡 Hint
Think about efficient min retrieval after each update.
✗ Incorrect
Min Stack allows O(1) min retrieval by tracking minimums during push/pop, suitable for real-time streams with element expiration.
🧠 Conceptual
expertSpace Complexity of Min Stack Design
What is the worst-case space complexity of a Min Stack that stores all elements and tracks minimums using an auxiliary stack?
Attempts:
2 left
💡 Hint
Consider how many elements the auxiliary min stack can hold in the worst case.
✗ Incorrect
In worst case, each pushed element is smaller or equal to previous min, so auxiliary stack stores all elements, leading to O(n) space.
