← Back
00:00

Min Stack

Swiggy interview questions · Easy · Coding round · 6 tests

Build a stack class offering the usual push, pop and top operations, plus a query that returns the smallest stored value, with every operation taking constant time.

Interface

class MinStack {
  push(val: number): void;
  pop(): void;
  top(): number;
  getMin(): number;
}

Requirements

  • push(val) places val on top of the stack.
  • pop() discards whatever is on top.
  • top() gives back the value currently on top without removing it.
  • getMin() gives back the smallest value among everything currently in the stack.
  • All four operations must be O(1).
Idle