LeetCode 716, Hard. Topics: Linked List, Stack, Design, Doubly-Linked List, Ordered Set. View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 61 parametrized pytest cases, and a playground notebook:
Problem
Design a max stack data structure that supports the stack operations and supports finding the stack’s maximum element.
Implement the MaxStack class:
MaxStack() Initializes the stack object.
void push(int x) Pushes element x onto the stack.
int pop() Removes the element on top of the stack and returns it.
int top() Gets the element on the top of the stack without removing it.
int peekMax() Retrieves the maximum element in the stack without removing it.
int popMax() Retrieves the maximum element in the stack and removes it. If there is more than one maximum element, only remove the top-most one.
You must come up with a solution that supports O(1) for each top call and O(logn) for each other call.
Examples
Constraints
- -10^7 <= x <= 10^7
- At most 10^5 calls will be made to push, pop, top, peek_max, and pop_max.
- There will be at least one element in the stack when pop, top, peek_max, or pop_max is called.
Solution
Reference implementation from solution.py on GitHub, full suite in test_solution.py:
Complexity
NeetCode All. Last modified on September 7, 2026