Dsa
Stack
Medium
Custom Stack Min Operation
Implement a custom stack that supports the following operations: 'push', 'pop', 'top', and 'get_min'. Each operation must run in O(1) time. The 'push' operation adds an integer to the stack, 'pop' removes the top integer, 'top' returns the top integer, and 'get_min' returns the minimum integer in the stack. If 'get_min' is called on an empty stack, return None.
Input Format:
- Multiple lines where the first line is an integer n, the subsequent n lines contain commands: 'push x', 'pop', 'top', and 'get_min'.
Output Format:
- For each command that returns a value (top and get_min), print the value.
Example:
Input:
5
push 3
push 5
get_min
top
pop
get_min
Output:
3
5
3
Key concepts
stackminimumdata 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