Code Logo

Minimum Stack Tracker

Published at23 Jul 2026
Medium 2 views
Like0

Design a data structure that behaves like a standard stack but also supports retrieving the minimum element in constant time. You must implement four operations:

  • push(val) — pushes an element onto the stack
  • pop() — removes the element on top of the stack
  • top() — returns the top element
  • getMin() — retrieves the minimum element in the stack

All four operations must run in O(1) time. The challenge is that the minimum changes as elements are pushed and popped, and a simple variable tracking the current minimum is insufficient because when the minimum is popped, you need to know what the previous minimum was.

The solution uses an auxiliary stack that tracks the minimum at each state. Every time you push a value, you also push the current minimum onto the auxiliary stack. When you pop, you pop from both stacks. This way, the auxiliary stack always has the minimum for the current state at its top.

Example Input & Output

Example 1
Input
push(3), push(5), getMin(), push(2), push(1), getMin(), pop(), getMin()
Output
3, 1, 2
Explanation

After pushing 3,5, min is 3. After pushing 2,1, min is 1. After popping 1, min reverts to 2.

Example 2
Input
push(-2), push(0), push(-3), getMin(), pop(), getMin()
Output
-3, -2
Explanation

Initial min is -2. After pushing -3, min becomes -3. After popping -3, min reverts to -2.

Example 3
Input
push(1), push(2), top(), getMin(), pop(), getMin()
Output
2, 1, 1
Explanation

After pushing 1 then 2, top is 2 and min is 1. After popping 2, top becomes 1 and min stays 1.

Algorithm Flow

Recommendation Algorithm Flow for Minimum Stack Tracker
Recommendation Algorithm Flow for Minimum Stack Tracker

Solution Approach

The two-stack approach is the most intuitive. The main stack stores all values. The min stack stores the minimum at each step. When pushing, compare the new value with the top of the min stack and push the smaller one onto the min stack. When popping, pop from both stacks. The getMin operation simply returns the top of the min stack.

class MinStack {
  constructor() {
    this.stack = []
    this.minStack = []
  }
  push(val) {
    this.stack.push(val)
    if (!this.minStack.length || val <= this.minStack[this.minStack.length - 1]) {
      this.minStack.push(val)
    }
  }
  pop() {
    const val = this.stack.pop()
    if (val === this.minStack[this.minStack.length - 1]) {
      this.minStack.pop()
    }
  }
  top() { return this.stack[this.stack.length - 1] }
  getMin() { return this.minStack[this.minStack.length - 1] }
}

Time complexity is O(1) for all operations. Space complexity is O(n) for the two stacks combined.

Best Answers

java
import java.util.*;
class MinStack {
    Stack<Integer> stack = new Stack<>();
    Stack<Integer> minStack = new Stack<>();
    public void push(int val) {
        stack.push(val);
        if (minStack.isEmpty() || val <= minStack.peek()) minStack.push(val);
    }
    public void pop() {
        if (!stack.isEmpty()) {
            int val = stack.pop();
            if (!minStack.isEmpty() && val == minStack.peek()) minStack.pop();
        }
    }
    public int top() { return stack.peek(); }
    public int getMin() { return minStack.peek(); }
}