Min Max Stack

MediumAppleMicrosoft

Prompt

Implement a MinMaxStack class that works like a regular stack but also lets you retrieve the current minimum and maximum values at any time, without removing anything.

Your class should support:

  • push(number) — adds a number to the top
  • pop() — removes and returns the top number
  • peek() — returns the top number without removing it
  • isEmpty() — returns true if the stack is empty
  • getMin() — returns the smallest number currently in the stack
  • getMax() — returns the largest number currently in the stack

getMin() and getMax() must work in constant time (no looping through the whole stack).