Maximum Frequency Stack - Problem
Design a Frequency Stack that efficiently handles push and pop operations based on element frequency.

You need to implement a special stack-like data structure called FreqStack with the following operations:
  • FreqStack(): Initialize an empty frequency stack
  • push(int val): Add an integer to the stack
  • pop(): Remove and return the most frequent element

The key challenge is handling tie-breaking: when multiple elements have the same highest frequency, return the one that was pushed most recently among those tied elements.

Example:
FreqStack freqStack = new FreqStack();
freqStack.push(5); // stack: [5]
freqStack.push(7); // stack: [5,7]
freqStack.push(5); // stack: [5,7,5]
freqStack.pop(); // returns 5 (frequency 2, most recent)
freqStack.pop(); // returns 7 (frequency 1)

Input & Output

example_1.py โ€” Basic Operations
$ Input: ["FreqStack", "push", "push", "push", "push", "pop", "pop", "pop", "pop"] [[], [5], [7], [5], [4], [], [], [], []]
โ€บ Output: [null, null, null, null, null, 5, 7, 5, 4]
๐Ÿ’ก Note: After pushing 5,7,5,4: frequencies are 5โ†’2, 7โ†’1, 4โ†’1. Pop returns 5 (freq 2, most recent), then 7 (freq 1), then 5 (freq 1), then 4 (freq 1).
example_2.py โ€” Frequency Ties
$ Input: ["FreqStack", "push", "push", "push", "pop", "pop", "pop"] [[], [1], [2], [1], [], [], []]
โ€บ Output: [null, null, null, null, 1, 2, 1]
๐Ÿ’ก Note: After pushing 1,2,1: frequency of 1 is 2, frequency of 2 is 1. Pop returns most recent 1, then 2, then remaining 1.
example_3.py โ€” Single Element
$ Input: ["FreqStack", "push", "push", "push", "pop", "pop", "pop"] [[], [3], [3], [3], [], [], []]
โ€บ Output: [null, null, null, null, 3, 3, 3]
๐Ÿ’ก Note: Pushing the same element three times creates frequencies 1, 2, 3. Popping returns them in reverse order of push (LIFO within same element).

Constraints

  • 1 โ‰ค val โ‰ค 109
  • At most 2 * 104 calls will be made to push and pop
  • pop is guaranteed to be called on a non-empty stack

Visualization

Tap to expand
๐Ÿฝ๏ธ VIP Restaurant System๐Ÿ‘‘ VIP Level 252 visitsโญ VIP Level 17451 visit each๐Ÿ“‹ Visit CounterCustomer 5: 2 visitsCustomer 7: 1 visitCustomer 4: 1 visitService Process:1. Always serve from highest VIP level first2. Within same level, serve most recent customer3. Customer 5 (VIP Level 2) gets served first4. Then Customer 4 (most recent in Level 1)NEXT
Understanding the Visualization
1
Customer Arrives
When a customer enters, we check their visit history and increment their frequency count
2
Assign VIP Level
Based on frequency, we assign them to the appropriate VIP section (frequency stack)
3
Seating Order
Within each VIP level, customers are seated in order of arrival (stack structure)
4
Service Priority
When serving, we always serve from the highest VIP level, taking the most recent customer from that level
Key Takeaway
๐ŸŽฏ Key Insight: By grouping elements into frequency-based stacks, we achieve O(1) operations while naturally handling both frequency priority and recency tie-breaking through the stack's LIFO property.
Asked in
Google 15 Amazon 12 Meta 8 Apple 6
89.5K Views
Medium-High 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