DSA stack learning path Level 2 This level · 3 of 5
Medium dsa stack

Min Stack

The 30 second answer

I keep two stacks: one for the values and one for the minimum that was in force after each push. Push records the new value and min(value, current min). Pop, top and getMin then all run in constant time, because the current minimum is always the top of the second stack.

Could you say this in an interview?

Problem Design a stack that supports push, pop, top and retrieving the minimum element, all in constant time. Original: LeetCode 155: Min Stack.

Understanding the prompt The operations must each be O(1), including getMin, so you cannot scan the stack on each query. Pop has to restore the previous minimum, not only the previous value. The official problem guarantees that pop, top and getMin are not called on an empty stack.

Pattern recognition An augmented stack: each entry carries the minimum of the suffix below it, or a second stack stores those minima in lockstep.

Brute force Store the values in a list and scan for the min on every getMin: O(n) queries, which fails the brief.

Optimal Two ArrayDeques used as stacks, vals and mins. On push, push the value and also push min(val, mins.peek()), or the value itself when mins is empty. pop removes both tops. top and getMin peek vals and mins. You can also store pairs in one stack; the two-stack form is slightly easier to read.

Alternatives Store the difference from the current min in one stack and keep a single min field. That saves space but is easier to get wrong with negatives, so prefer two stacks in an interview unless you are asked to use O(1) extra words.

Java solution

import java.util.ArrayDeque;

public class MinStack {
    private final ArrayDeque<Integer> vals = new ArrayDeque<>();
    private final ArrayDeque<Integer> mins = new ArrayDeque<>();

    void push(int val) {
        vals.push(val);
        mins.push(mins.isEmpty() ? val : Math.min(val, mins.peek()));
    }

    void pop() {
        vals.pop();
        mins.pop();
    }

    int top() {
        return vals.peek();
    }

    int getMin() {
        return mins.peek();
    }

    public static void main(String[] args) {
        var s = new MinStack();
        s.push(-2);
        s.push(0);
        s.push(-3);
        System.out.println(s.getMin()); // -3
        s.pop();
        System.out.println(s.top());    // 0
        System.out.println(s.getMin()); // -2
    }
}

Complexity Every operation is O(1) time. O(n) extra space for the second stack.

Edge cases

  • The official sequence push -2, push 0, push -3, getMin, pop, top, getMin returns -3, then 0, then -2.
  • Pushing a new equal minimum: both stacks still record it, so two pops are needed before the previous minimum returns.
  • A single element: getMin equals top.

Follow-ups

  • Max stack is the same idea with max instead of min.
  • A queue with getMin is harder: you want a monotonic deque, not a min stack.

Tests After push(-2), push(0), push(-3), getMin returns -3; then pop, top returns 0 and getMin returns -2.

How to say it in the interview "I keep a second stack of the minimum after each push. When I push, I also push the min of the new value and the current minimum. Pop, top and getMin then all look only at the tops, so they are constant time."

Sources (3)
  1. LeetCode 155: Min Stackleetcode.com
  2. Java SE 17 API: ArrayDequedocs.oracle.com
  3. Java SE 17 API: Math.mindocs.oracle.com
Previous
Next