Skip to content
Algorithms for Beginners

Problems / Stack

Stack with minimum

medium

New here? How this page works
  1. Read the Problem tab. Read it twice. Look at the examples: what goes in, what should come out.
  2. Open Break it down. Answer the questions in your own words. This is your plan.
  3. Write code on the right. Start small. Stuck on a blank page? Press “Write my plan as comments”.
  4. Press Run examples. See which examples pass. Red ones show what your code gave and what was expected.
  5. Stuck? Use Hints, one at a time. Still stuck after all of them? Read the Walkthrough. That's learning, not cheating.
  6. Press Submit. It runs hidden tests too. Pass them all, and it's solved.

Write a class MinStack: a stack of numbers with four methods.

push(n) puts n on top. pop() removes the top item (it doesn't need to return it). top() returns the top item. min() returns the smallest item in the whole stack.

Every method must be instant, O(1): min() can't look through the whole stack.

The tests build a new MinStack, call its methods in order, and check what each call returns. Calls that return nothing are shown as null.

Examples

  1. Example 1

    Input
    calls = [["push", 5], ["push", 2], ["push", 8], ["min"], ["pop"], ["top"], ["pop"], ["min"]]
    Output
    [null, null, null, 2, null, 2, null, 5]

    After pushing 5, 2, 8 the smallest is 2. Pop 8: the top is 2. Pop 2: now the smallest is 5.

  2. Example 2

    Input
    calls = [["push", 3], ["push", 3], ["pop"], ["min"]]
    Output
    [null, null, null, 3]

    Two 3s: popping one still leaves a 3 as the smallest.

Rules

  • pop, top and min are only called when the stack isn't empty.
  • Up to 150,000 calls per test. Numbers can be negative, and can repeat.
Words on this page, in plain English (3)Open if any word is new to you
O(1): constant time
Takes the same time no matter how big the input is. Like opening a book at page 1.
Stack
A pile where you only touch the top: add with push, take with pop. The last thing in is the first out, like a pile of plates.
Test
An example input with the answer it should give. Your code passes if it gives that answer.

Every word, explained simply

Your solution

JavaScript · Press Esc then Tab to leave the editor

class MinStack {
  constructor() {
    // Set up whatever you need here.
  }

  push(n) {}

  pop() {}

  top() {}

  min() {}
}

Blank page fear? (uses your answers from Break it down; comments don’t run).

Results

Run the examples to check your code, then Submit to run every test.

Next steps in stack

  • Learn the stack pattern

    Keep the most recent unfinished thing on top, and finish it first.

  • Postfix calculator

    Medium. Work out a sum written with the operators after the numbers, like 3 4 + 2 *.

  • Tidy a file path

    Medium. Turn a messy folder path with dots and extra slashes into its simplest form.

  • Days until warmer

    Medium. For each day, count how many days you'd wait for a warmer one.

  • Balanced brackets

    Easy. Check whether every bracket in a string is closed by the right kind, in the right order.

  • Typing with backspace

    Easy. Work out what's on the screen after someone types, including presses of the backspace key.