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 Operations Sequence
What is the output printed by the following C++ code that uses a Min Stack?
DSA C++
#include <stack> class MinStack { private: std::stack<int> s, minS; public: void push(int x) { s.push(x); if(minS.empty() || x <= minS.top()) { minS.push(x); } } void pop() { if(s.top() == minS.top()) { minS.pop(); } s.pop(); } int top() { return s.top(); } int getMin() { return minS.top(); } }; #include <iostream> int main() { MinStack ms; ms.push(3); ms.push(5); std::cout << ms.getMin() << " "; ms.push(2); ms.push(1); std::cout << ms.getMin() << " "; ms.pop(); std::cout << ms.getMin() << " "; ms.pop(); std::cout << ms.top() << " "; return 0; }
Attempts:
2 left
💡 Hint
Track the minimum values pushed and popped carefully.
✗ Incorrect
The min stack keeps track of minimums. After pushing 3 and 5, min is 3. After pushing 2 and 1, min is 1. Popping once removes 1, min becomes 2. Popping again removes 2, top is 5.
❓ Predict Output
intermediateResulting Min Stack State After Operations
After executing the following operations on a Min Stack, what is the value returned by getMin()?
DSA C++
MinStack ms; ms.push(10); ms.push(20); ms.push(5); ms.pop(); ms.push(7); ms.pop(); ms.pop(); return ms.getMin();
Attempts:
2 left
💡 Hint
Remember to update min when popping elements.
✗ Incorrect
Push 10 (min=10), push 20 (min=10), push 5 (min=5), pop removes 5 (min=10), push 7 (min=7), pop removes 7 (min=10), pop removes 20 (min=10). So min is 10.
🔧 Debug
advancedIdentify the Bug in Min Stack Implementation
What error will the following Min Stack code produce when pop() is called on an empty stack?
DSA C++
#include <stack> class MinStack { private: std::stack<int> s, minS; public: void push(int x) { s.push(x); if(minS.empty() || x <= minS.top()) { minS.push(x); } } void pop() { if(s.top() == minS.top()) { minS.pop(); } s.pop(); } int top() { return s.top(); } int getMin() { return minS.top(); } }; int main() { MinStack ms; ms.pop(); return 0; }
Attempts:
2 left
💡 Hint
Consider what happens when calling top() or pop() on an empty stack.
✗ Incorrect
Calling top() or pop() on an empty std::stack causes undefined behavior, often a segmentation fault.
🧠 Conceptual
advancedSpace Complexity of Min Stack Design
What is the worst-case space complexity of a Min Stack implemented with two stacks (one for values, one for minimums) when pushing n elements?
Attempts:
2 left
💡 Hint
Consider how many elements can be stored in the min stack in the worst case.
✗ Incorrect
In the worst case, every pushed element is smaller or equal to the current minimum, so min stack stores all n elements, resulting in O(n) space.
🚀 Application
expertUsing Min Stack to Find Minimum in Sliding Window
Given a Min Stack implementation, which approach correctly uses it to find the minimum in every sliding window of size k over an array of integers?
Attempts:
2 left
💡 Hint
Think about how to maintain the window elements in the stack and update minimum accordingly.
✗ Incorrect
By pushing elements entering the window and popping those leaving, the Min Stack always holds current window elements, so getMin() returns the window minimum.
