Min Stack Challenge

Stack Data Structures Min Stack Coding Challenge

Min Stack Challenge

Implement a stack that supports retrieving the minimum element in constant time.

Your task is to design a data structure, MinStack, that supports the following operations:

  • push(x): Push element x onto the stack.
  • pop(): Remove the element on top of the stack.
  • top(): Get the top element.
  • getMin(): Retrieve the minimum element in the stack.

All operations should run in constant time, O(1).

Example:

MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin();   // Returns -3.
minStack.pop();
minStack.top();      // Returns 0.
minStack.getMin();   // Returns -2.

Hint: Consider using an auxiliary stack to keep track of minimum values.

On February 6, we celebrate persistence and innovation in technology. Use this challenge as your building block to stack up your coding skills!