Solvarch
DSA ProblemsProgressPlanRoadmapsReviewCheat SheetsCompaniesSystem DesignCS FundamentalsQuestion Bank
Theme
Problems

Min Stack

Medium
stackdesign

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

Input: one operation per line —

  • push x — push integer x
  • pop — remove the top
  • top — read the top
  • getMin — read the minimum

Output: one line per operation — push and pop print null; top and getMin print their result. Operations are always valid (no pop/top on an empty stack).

Example 1
Input:
push -2
push 0
push -3
getMin
pop
top
getMin
Output:
null
null
null
-3
null
0
-2

Constraints

  • Up to 3·10^4 operations
  • -2^31 ≤ x ≤ 2^31 − 1
Watch a video walkthrough

Run your code against the sample tests, or Submit to run the full suite.