Min Stack

Medium · Stack & Strings · Stack · Design

Problem

Design a stack that also reports its smallest element, with every operation running in constant time:

Your tests replay a sequence of calls: the first names the class to construct, and each later entry is a method and its arguments. Operations that return nothing report None.

Examples

Input: push(-2), push(0), push(-3), getMin(), pop(), top(), getMin()
Output: -3, then 0, then -2

getMin() returns -3 while it is on the stack, and -2 once it has been popped.

Constraints

Approaches

Related problems

Start free on Cariara · Coding problems · System design studio

Starting Python
No results yet.

          
↑↓ move ↵ open esc close

Labs

Loading labs…

Run with your own input

Starting Python

Loading Python…