Min Stack
Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.
Practice this as a mock interviewFree to start, no account neededProblem
Design a stack that, in addition to the usual operations, can report its current minimum element — all in **O(1)** time per call.
Implement:
- MinStack() creates an empty stack.
- push(val) pushes val onto the stack.
- pop() removes the top element (returns nothing).
- top() returns the top element.
- getMin() returns the smallest element currently in the stack.
pop, top and getMin are only called when the stack is non-empty. Duplicate values are allowed, and popping one copy of the minimum must not lose a remaining copy.
Examples
Constraints
- -2^31 <= val <= 2^31 - 1
- `pop`, `top` and `getMin` are only called on a non-empty stack.
- At most 3 * 10^4 calls are made in total.
What the interviewer grades
A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.