Tech Interview RoomStudy guides

Stack & Queue

Design a stack that returns the minimum in O(1). Compare scanning, two stacks and encoded values.

Intermediate · 2 min read · Lesson 3 of 4 in Stack & Queue

Problem

Support push, pop, top and getMin, all in O(1).

Pattern

Auxiliary state per stack level: store, alongside each element, what the answer would be if that element were the top.

Approach 1: Scan on getMin (feasible, O(n) getMin)

java
import java.util.ArrayDeque;
import java.util.Deque;

class MinStackScan {
    private final Deque<Integer> stack = new ArrayDeque<>();
    void push(int x) { stack.push(x); }
    int pop() { return stack.pop(); }
    int getMin() { return stack.stream().mapToInt(Integer::intValue).min().orElseThrow(); }
}

Approach 2: Two stacks (O(1) for everything)

A second stack holds the current minimum at each level (push only when ≤ the current minimum to save space).

java
import java.util.ArrayDeque;
import java.util.Deque;

class MinStackTwoStacks {
    private final Deque<Integer> stack = new ArrayDeque<>(), mins = new ArrayDeque<>();
    void push(int x) {
        stack.push(x);
        if (mins.isEmpty() || x <= mins.peek()) mins.push(x);
    }
    int pop() {
        int x = stack.pop();
        if (x == mins.peek()) mins.pop();
        return x;
    }
    int top() { return stack.peek(); }
    int getMin() { return mins.peek(); }
}

Approach 3: One stack of (value, minSoFar) records (Java 16+ records, O(1))

Simplest to get right; one allocation per push.

java
import java.util.ArrayDeque;
import java.util.Deque;

class MinStackRecords {
    private record Entry(int value, int min) {}
    private final Deque<Entry> stack = new ArrayDeque<>();
    void push(int x) { stack.push(new Entry(x, stack.isEmpty() ? x : Math.min(x, stack.peek().min()))); }
    int pop() { return stack.pop().value(); }
    int top() { return stack.peek().value(); }
    int getMin() { return stack.peek().min(); }
}

class MinStackDemo {
    public static void main(String[] args) {
        MinStackScan a = new MinStackScan(); MinStackTwoStacks b = new MinStackTwoStacks(); MinStackRecords c = new MinStackRecords();
        for (int x : new int[] {5, 3, 7, 3, 1}) { a.push(x); b.push(x); c.push(x); }
        System.out.println(a.getMin() + " " + b.getMin() + " " + c.getMin());   // 1 1 1
        a.pop(); b.pop(); c.pop();
        System.out.println(a.getMin() + " " + b.getMin() + " " + c.getMin());   // 3 3 3
        a.pop(); b.pop(); c.pop();
        System.out.println(a.getMin() + " " + b.getMin() + " " + c.getMin());   // 3 3 3 (the other 3 is still there)
    }
}

Feasible → optimal

Scanning makes getMin O(n). Two stacks or a stack of records make every operation O(1); records are the clearest. (An O(1)-extra-space trick stores 2x − min when pushing a new minimum, but it risks overflow and is rarely worth it.)

Follow-up questions interviewers ask

Try answering each one before you open it.

Related (same pattern): implement a queue using two stacks.

Push onto an in stack; when popping, if the out stack is empty, move everything from in to out (reversing the order) and pop from out. Each element moves at most once, so operations are amortised O(1).

Related (same pattern): max stack or a queue that reports its maximum.

Max stack: same as the min stack with max. Max queue: implement the queue with two max-stacks (the answer is the max of both stacks' maxima), or keep a monotonic deque alongside the queue, as in sliding window maximum.