Explanation
Min Stack is a Medium interview problem tagged [Stack, Design]. The winning approach is typically **Design**.
#155
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
You must implement a solution with O(1) time complexity for each function.
Example 1
Input: MinStack minStack = new MinStack(); minStack.push(-2); minStack.push(0); minStack.push(-3); minStack.getMin(); minStack.pop(); minStack.top(); minStack.getMin();
Output: [null,null,null,null,-3,null,0,-2]
After pushing -3, min is -3; after pop, top is 0 and min is -2.
Min Stack is a Medium interview problem tagged [Stack, Design]. The winning approach is typically **Design**.
Each structure covers a weakness of the other (lookup vs order).
Combine primitives (hash map + doubly linked list, two heaps, etc.) to hit the required complexities.
Pattern-level skeleton only — not this problem’s exact answer. Adapt it to the statement.
Time: O(n log n) or O(n)
Space: O(n)
Before coding, restate the problem, name the pattern (Design), and write the invariant or DP state in one sentence.
Got it?
Mark #155 Min Stack as understood when the pattern clicks.