Maximum Frequency Stack - Problem

Design a stack-like data structure to push elements to the stack and pop the most frequent element from the stack.

Implement the FreqStack class:

  • FreqStack() constructs an empty frequency stack.
  • void push(int val) pushes an integer val onto the top of the stack.
  • int pop() removes and returns the most frequent element in the stack. If there is a tie for the most frequent element, the element closest to the stack's top is removed and returned.

Input & Output

Example 1 — Basic Operations
$ Input: operations = ["FreqStack", "push", "push", "push", "push", "push", "push", "pop", "pop", "pop", "pop"], values = [null, 5, 7, 5, 7, 4, 5, null, null, null, null]
Output: [null, null, null, null, null, null, null, 5, 7, 5, 4]
💡 Note: After pushes: 5(freq=3), 7(freq=2), 4(freq=1). Pop returns most frequent: 5, then 7, then 5, then 4.
Example 2 — Tie Breaking
$ Input: operations = ["FreqStack", "push", "push", "push", "pop", "pop"], values = [null, 1, 2, 3, null, null]
Output: [null, null, null, null, 3, 2]
💡 Note: All elements have frequency 1, so pop returns most recent: 3, then 2.
Example 3 — Same Element Multiple Times
$ Input: operations = ["FreqStack", "push", "push", "pop", "pop"], values = [null, 1, 1, null, null]
Output: [null, null, null, 1, 1]
💡 Note: Push 1 twice (freq=2), pop returns 1 (freq becomes 1), pop returns 1 again.

Constraints

  • 1 ≤ val ≤ 109
  • At most 2 × 104 calls to push and pop
  • It is guaranteed that there will be at least one element in the stack before calling pop

Visualization

Tap to expand
Maximum Frequency Stack INPUT Operations: FreqStack, push, push, push, push, push, push, pop, pop, pop, pop Values pushed: 5, 7, 5, 7, 4, 5 Stack After All Pushes: 5 (1st) 7 (1st) 5 (2nd) 7 (2nd) 4 (1st) 5 (3rd) TOP ALGORITHM STEPS 1 Track Frequencies freq[val] = count of val 2 Group by Frequency group[freq] = stack of vals 3 Track Max Frequency maxFreq = highest freq 4 Pop from Max Group Return group[maxFreq].pop() Data Structure State: freq map 5 --> 3 7 --> 2 4 --> 1 maxFreq = 3 group stacks 1: [5,7,4] 2: [5,7] 3: [5] <--pop Pop Sequence: 5(f=3), 7(f=2), 5(f=2), 4(f=1) Ties: most recent wins FINAL RESULT Output Array: [null, null, null, null, null, null, 5, 7, 5, 4] Pop Results Visualization: 5 pop 1 7 pop 2 5 pop 3 4 pop 4 Time Complexity: push: O(1), pop: O(1) Space: O(n) OK - All Operations Executed Successfully Each pop returns element with highest frequency Key Insight: Use TWO hash maps: one tracks frequency of each value, another groups values by their frequency. Each frequency level maintains a stack, so ties naturally resolve to the most recent push. Pop from the stack at maxFreq level. This achieves O(1) for both push and pop operations. TutorialsPoint - Maximum Frequency Stack | Optimal Solution
Asked in
Google 15 Amazon 12 Microsoft 8 Facebook 6
89.5K Views
Medium Frequency
~25 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen