Min Stack - Problem

Imagine you're building a smart stack that not only stores elements but also instantly knows its minimum value at all times! This is more challenging than it sounds because stacks only allow access to the top element.

Your task is to design a MinStack class that supports these operations:

  • push(val) - Add an element to the top
  • pop() - Remove the top element
  • top() - Get the top element
  • getMin() - Get the minimum element

The catch? All operations must run in O(1) constant time! No scanning through the entire stack allowed.

Example:

MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); // return -3
minStack.pop();
minStack.top();    // return 0
minStack.getMin(); // return -2

Input & Output

example_1.py — Basic Operations
$ Input: ["MinStack","push","push","push","getMin","pop","top","getMin"] [[],[-2],[0],[-3],[],[],[],[]]
Output: [null,null,null,null,-3,null,0,-2]
💡 Note: MinStack minStack = new MinStack(); minStack.push(-2); minStack.push(0); minStack.push(-3); minStack.getMin(); // return -3; minStack.pop(); minStack.top(); // return 0; minStack.getMin(); // return -2
example_2.py — Duplicate Minimums
$ Input: ["MinStack","push","push","push","getMin","pop","getMin"] [[],[1],[1],[1],[],[],[]]
Output: [null,null,null,null,1,null,1]
💡 Note: When all elements are the same, the minimum remains constant. After popping one element, the minimum is still 1.
example_3.py — Ascending Order
$ Input: ["MinStack","push","push","push","getMin","pop","getMin","pop","getMin"] [[],[3],[2],[1],[],[],[],[],[]]
Output: [null,null,null,null,1,null,2,null,3]
💡 Note: Elements pushed in descending order. Each pop operation reveals the next minimum in the remaining stack.

Constraints

  • -231 <= val <= 231 - 1
  • Methods pop, top and getMin operations will always be called on non-empty stacks
  • At most 3 × 104 calls will be made to push, pop, top, and getMin

Visualization

Tap to expand
Min Stack - Optimal Solution INPUT Operations: push(-2) push(0) push(-3) getMin(), pop(), top(), getMin() Stack after pushes: -3 top 0 -2 Min Stack -3 -2 -2 Main stack stores values Min stack tracks minimums ALGORITHM STEPS 1 Use Two Stacks Main stack + Min stack 2 Push Operation Push to main; update min stack 3 Pop Operation Pop from both stacks 4 GetMin Operation Return min stack top - O(1) push(val): mainStack.push(val) if minStack empty or val <= minStack.top: minStack.push(val) getMin(): return minStack.top Time: O(1) for all ops Space: O(n) FINAL RESULT Operation Trace: MinStack() --> null push(-2) --> null push(0) --> null push(-3) --> null getMin() --> -3 pop() --> null top() --> 0 getMin() --> -2 (min updated after pop!) Output Array: [null,null,null,null,-3,null,0,-2] OK - All O(1)! All operations completed in constant time complexity. Key Insight: The auxiliary min stack maintains the minimum element at each level of the main stack. When we push, we only add to min stack if the new value is less than or equal to current min. This ensures getMin() always returns in O(1) by simply peeking at the min stack's top element. TutorialsPoint - Min Stack | Optimal Solution (Two Stacks)
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28
98.0K Views
Very High Frequency
~15 min Avg. Time
2.2K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen