Dsa
Stack
Easy
Max Stack for Maximum Value
Implement a stack that supports push, pop, top, and retrieving the maximum element in constant time. Your stack should support the following operations:
1. push(x): Pushes element x onto the stack.
2. pop(): Removes the element on the top of the stack.
3. top(): Gets the top element of the stack without removing it.
4. get_max(): Retrieves the maximum element in the stack.
Example:
Input:
push(1)
push(3)
push(2)
get_max()
pop()
get_max()
top()
Output:
3
2
2
Key concepts
stackdata-structuremaximum
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