57 / 75
M

Min Stack

#57
stackdesign

Use two stacks: one for values, one tracking minimum at each level.

Example:

Input:push(-2), push(0), push(-3), getMin(), pop(), top(), getMin()
Output:[null, null, null, -3, null, 0, -2]

Common Mistakes:

  • Only storing single min value (fails when min is popped)
  • Not handling duplicate minimums correctly
  • Using == instead of equals() for Integer comparison
  • Trying to find min by scanning stack (not O(1))

Notes:

All operations O(1) time. Space O(n) for two stacks. Alternative: store pairs (value, min) in single stack.

57/75
Min Stack