Dsa
Stack
Warm-up
Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. Implement the MinStack class with the following methods:
- `push(int val)`: Pushes the element val onto the stack.
- `pop()`: Removes the element on the top of the stack.
- `top()`: Gets the top element of the stack.
- `get_min()`: Retrieves the minimum element in the stack.
Input Format:
A series of operations (push, pop, etc.) followed by their parameters in a single line.
Output Format:
After each operation, print the current top and min values if applicable.
Example:
Input:
push 5
push 2
get_min
pop
top
get_min
Output:
2
5
5
Key concepts
stackdesigndata structure
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now