Problem, as an interviewer would state it:
"Design a stack supporting push, pop, top, and getMin — all in O(1) time, including getMin, which must return the current minimum value in the stack at any point."
Examples:
const s = new MinStack();
s.push(5); s.push(2); s.push(7);
s.getMin(); // 2
Clarifying questions expected:
- Is a genuine O(1) time requirement for
getMinspecifically the real, hard constraint here — would scanning the whole stack on each call be acceptable? - After popping the CURRENT minimum off the stack, does
getMinneed to correctly revert to the PREVIOUS minimum? - Should duplicate values (the same number pushed twice) be handled correctly if one of them happens to be the current minimum?
Code / implementation expected: Yes — real, direct proof that getMin runs in O(1) and correctly reverts to the previous minimum after the current minimum is popped off.