Skip to main content
← Problem catalogue Understand the pattern. Try it yourself. Review the solution. Practice this pattern →
Medium Stack High frequency Solution Available Practice Mode

Min Stack LeetCode Solution

Tags: #Stack #Design

Approach Summary

Maintain two stacks: main stack and min stack. Push to min stack only when new value <= current minimum.

Practice Mode

Solve it yourself before revealing the code

DSA interviews test reasoning from brute force to optimal. Think through the constraints first.

Algorithmic Pattern

Master the Stack Pattern

View Pattern Guide →

Study the core invariant, step-by-step decision framework, and common interview traps for the Stack pattern.

Framework: Stack • Interview Prep: Tested at FAANG • Explore all Stack problems & invariants →

How to Recognize This Pattern

  • Stack with O(1) minimum retrieval

Complexity Analysis

Time Complexity

O(1) all ops

Space Complexity

O(n)

Tags

Stack Design

Frequently Asked Questions

What is the optimal time and space complexity for Min Stack?
The optimal solution for Min Stack achieves O(1) all ops runtime complexity and requires O(n) auxiliary space. Every operation does constant work — a push or pop plus an O(1) min comparison — so all four methods are O(1). Space is O(n) for the two stacks, bounded by the number of pushed values.
How do I identify that Min Stack requires the Stack pattern?
In technical interviews, look for key problem signals: Stack with O(1) minimum retrieval. Recognizing these invariants allows you to bypass naive brute-force exhaustive search in favor of Stack strategy.
What critical edge cases must be handled in Min Stack?
Essential edge cases to verify include: Repeated minimum values — mins must store duplicates, e.g. push 5, 3, 3 keeps [5, 3, 3]; Push after pop — mins stays in sync because both stacks are popped together; getMin on a fresh stack — the problem guarantees no top/getMin on an empty stack. Always walk through these scenarios with your interviewer before coding.
Which tech companies frequently ask Min Stack?
Min Stack is a Medium-difficulty question tagged with high recurrence. It is actively asked in technical screening and onsite interview loops at Amazon, Bloomberg, Google.
Buy me a coffee
Daily Practice
0/0