v9.1.1
Menu

Min Stack

mediumPRODesign & Hash MapsUber DSA

Design a stack that supports push, pop, top, and retrieving the minimum element in O(1) time.

Input: operations separated by semicolons. Output results of top, getMin operations.

Example

Input
push,-2;push,0;push,-3;getMin;pop;top;getMin
Output
-3
0
-2
Reasoning
  • We start with an empty stack and apply the operations in sequence: push -2, push 0, push -3.
  • The getMin operation returns the current minimum element, which is -3, so the first output is −3-3.
  • We then pop the top element (-3), and the top operation returns the new top element, which is 0.
  • After the top operation, another getMin operation is performed, returning the new minimum element, which is -2.

Constraints

  • -2^31 <= val <= 2^31 - 1
  • pop, top, getMin always called on non-empty stack

More from Uber DSA

PRO

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test results

Sign in to run your code against the test cases.