SystemTutor
About
Sign in
Loading course documentation…
SystemTutor

COURSES

  • Data Structures & Algorithms
  • CSS MasterclassNew course
  • System DesignNew course

PRACTICE

  • Algorithm practice
  • CSS practice

LEGAL

  • Privacy Policy
  • Terms of Service
  • About

© 2026 SystemTutor. All rights reserved.

Built with 🫶 by Shatlyk

Course Overview
Singly Linked ListReverse Linked ListDoubly Linked ListData-Structure and Linked-List Trade-offsMerge Two Sorted ListsRemove Nth Node From End of ListStackValid ParenthesesMin StackDaily TemperaturesQueueImplement Queue Using StacksCircular QueuesCircular Queues QuizDesign Circular QueueSliding Window MaximumQueues, Circular Buffers, and Monotonic Deques
Linked Lists, Stacks & Queues

Min Stack

Design a stack that reports its minimum value in constant time.

Try It Yourself

Use the editor below to process MinStack operations.

Use constant-time stack operations

Track minimum history as values are pushed and popped so getMin takes O(1) time. After removing one copy of a duplicate minimum, another copy must still count as the minimum.

Submit your implementation when it passes the examples.

Min Stack

mediumStack · Design30 minLC 155
Problem

Process a sequence of MinStack operations. push, pop, top, and getMin must all run in constant time.

Return one output per operation. Constructors, push, and pop produce null; top and getMin produce numbers.

Example 1:

Input: operations = ["MinStack", "push", "push", "push", "getMin", "pop", "top", "getMin"], values = [[], [-2], [0], [-3], [], [], [], []]
Output: [null, null, null, null, -3, null, 0, -2]
Explanation: After the pushes, the minimum is -3. Popping -3 leaves [-2, 0], so top returns 0 and getMin returns -2. The constructor, pushes, and pop each return null.

Example 2:

Input: operations = ["MinStack", "push", "getMin", "top"], values = [[], [5], [], []]
Output: [null, null, 5, 5]
Explanation: After pushing 5, it is the only item, so both getMin and top return 5. The constructor and push each return null.

Constraints

  • At most 30,000 operations are performed.
  • pop, top, and getMin are called only on a non-empty stack.
0 attempts

Test results

Results appear here after a run.

Solution

Values 5, 2, 7, 1 have running minima 5, 2, 2, 1. The minimum is read from the top of the second stack.

For every entry in the value stack, store the minimum at that depth in a second stack. On the first push, the minimum is the value itself. On later pushes, append the smaller of the new value and the previous minimum.

In the diagram, pushing 7 onto [5, 2] records another 2 in the minimum stack. Pushing 1 records 1. A pop removes one entry from each stack, restoring the minimum at the previous depth. The same rule handles duplicate minimum values.

function runMinStack(operations, values) {
  class MinStack {
    constructor() {
      this.items = [];
      this.minimums = [];
    }

    push(value) {
      this.items.push(value);
      const previous = this.minimums[this.minimums.length - 1];
      this.minimums.push(previous === undefined ? value : Math.min(value, previous));
    }

    pop() {
      this.items.pop();
      this.minimums.pop();
    }

    top() {
      return this.items[this.items.length - 1];
    }

    getMin() {
      return this.minimums[this.minimums.length - 1];
    }
  }

  let stack;
  const output = [];

  for (let index = 0; index < operations.length; index++) {
    const operation = operations[index];

    if (operation === "MinStack") {
      stack = new MinStack();
      output.push(null);
    } else if (operation === "push") {
      stack.push(values[index][0]);
      output.push(null);
    } else if (operation === "pop") {
      stack.pop();
      output.push(null);
    } else if (operation === "top") {
      output.push(stack.top());
    } else {
      output.push(stack.getMin());
    }
  }

  return output;
}

Big O notation

MeasureComplexityExplanation
TimeO(n)Across n operations, dynamic-array pushes and pops take amortized O(1) time each. getMin and top each read a single slot.
Auxiliary spaceO(n)The value and minimum stacks can each hold up to n values.

How is this practice?

Previous lessonValid ParenthesesNext lessonDaily Temperatures
Edit on GitHubReport on GitHub
On this page
%

On this page

Try It YourselfSolutionBig O notation