15% off all plans & roadmaps Use code
See plansLeetCode 155 · Solution & editorial
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
Min Stack is a Stack Patterns problem, in the Stack - Min Stack Design subpattern. The pattern covers parentheses matching, monotonic stack, histogram, and expression evaluation
Design a stack that supports push, pop, top, and retrieving minimum element in constant time.
The reference solution runs in O(1) time and O(n) space.
Min Stack has been reported in interviews at Capital One, Amazon, Bloomberg, Microsoft, Oracle, eBay, Apple, Coupang.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Min Stack is a Stack Patterns problem, in the Stack - Min Stack Design subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.