Skip to content
AI360Xpert
Beta
Difficulty: MediumStack

Min Stack

Problem in Plain English

Design a stack that supports push, pop, top and getMin, with every operation running in constant time. Storing the running minimum alongside each entry makes the minimum query a plain top-of-stack read.

Problem Statement

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.

Implement the MinStack class: MinStack() initializes the stack object, void push(int val) pushes val onto the stack, void pop() removes the top element, int top() returns the top element, and int getMin() retrieves the minimum element in the stack. Every method must run in O(1)O(1) time.

The constructor and push/pop produce null in the operation-sequence output. top and getMin return integers. Empty-stack queries and pops are outside the valid input contract.

Constraints

  • -2^31 <= val <= 2^31 - 1
  • Methods pop, top and getMin are always called on non-empty stacks.
  • At most 3 * 10^4 calls are made to push, pop, top and getMin.

Examples

Example 1

Input
["MinStack","push","push","push","getMin","pop","top","getMin"] [[],[-2],[0],[-3],[],[],[],[]]
Output
[null,null,null,null,-3,null,0,-2]

Push -2, 0, -3. getMin returns -3. After pop, top returns 0 and getMin returns -2.

Example 2

Input
["MinStack","push","push","getMin"] [[],[1],[2],[]]
Output
[null,null,null,1]

Push 1, 2. The minimum of the stack is 1.

Example 3

Input
["MinStack","push","top","getMin","pop","push","getMin"] [[],[2147483647],[],[],[],[-2147483648],[]]
Output
[null,null,2147483647,2147483647,null,null,-2147483648]

A one-element stack has the same top and minimum. After removing its only entry, pushing the smallest signed 32-bit value starts a fresh stack; no stale minimum survives.

Example 4

Input
["MinStack","push","push","push","getMin","pop","getMin","pop","top","getMin"] [[],[-2],[-2],[0],[],[],[],[],[],[]]
Output
[null,null,null,null,-2,null,-2,null,-2,-2]

The minimum occurs twice. Popping 0 leaves both copies; popping the newer -2 still leaves the older -2. A single minimum flag without multiplicity or history would lose this information.

Intuition

A stack remembers values in last-in-first-out order, but its smallest value may be buried below the top. In Example 1, pushing -3 changes the minimum from -2 to -3; popping -3 must restore -2. Store a snapshot of the minimum with each entry, so removing the newest entry exposes the correct older snapshot. Duplicate minima need their own snapshots too.

Approaches

Rescan For Minimum

Solution Details

Reveal Rescan For Minimum: intuition, complexity, and code

Hints

Hint 1
A plain stack already handles push, pop and top in constant time, so only getMin needs a new idea.
Hint 2
The minimum after a pop is simply the minimum from before the matching push, which suggests remembering history.
Hint 3
Store each value together with the minimum of everything up to it, then answer getMin from the top entry.

Edge Cases

  • Pushing duplicate minimums such as [-2, 0, -2]: popping one copy must leave the other minimum intact.
  • Popping the unique minimum must reveal the previous higher minimum stored underneath.
  • Interleaved pushes and pops must keep every pair aligned, since each pop discards exactly one stored minimum.
  • Emptying and refilling the stack must discard the old minimum; queries on an empty stack are never required.
  • The extreme values -2147483648 and 2147483647 work using comparisons; arithmetic encodings based on 2 * val - min can overflow a Java int.

Common Mistakes and Interview Tips

  • Keeping a single global minimum variable, which breaks the moment the entry holding that minimum is popped.
  • Scanning the stack inside getMin, which is correct but costs linear time and violates the constant-time requirement.
  • Returning the stored minimum from top or the stored value from getMin by mixing up the two halves of the pair.
  • If using a separate minima stack, recording only strictly smaller minima loses duplicate counts; use <= or store counts. Per-entry snapshots avoid that bug.
  • Calling an array-backed push worst-case constant time ignores resizing. State amortized bounds explicitly, or use fixed-size linked nodes for constant work per method.

Key Takeaway

When a last-in-first-out structure must answer a running property in constant time, store that property alongside each entry so push and pop maintain it for free.