15% off all plans & roadmaps Use code
See plansLeetCode 895 · Solution & editorial
Design a stack-like data structure to push elements to the stack and pop the most frequent element from the stack.
Maximum Frequency Stack is a Stack Patterns problem, in the Stack - Min Stack Design subpattern. The pattern covers parentheses matching, monotonic stack, histogram, and expression evaluation
Design a stack that supports push, pop, top, and retrieving minimum element in constant time.
The reference solution runs in O(1) time and O(n) space.
Maximum Frequency Stack has been reported in interviews at Amazon, Microsoft, Bloomberg, Adobe.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Maximum Frequency Stack is a Stack Patterns problem, in the Stack - Min Stack Design subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.