Maximum Frequency Stack
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Amazon reported this one in September 2026, and the detail that trips people is the pop rule: highest frequency wins, and ties go to whoever was pushed most recently. Example 1 pops 5, 7, 5, 4 from pushes of 5,7,5,7,4,5, and that sequence is the whole problem in miniature. It's a stack design question with a hash map twist, and it's very solvable in O(1) per operation once you see the structure. If you blank on the layout during the OA, StealthCoder runs invisibly on screen and gives you a working solution as a safety net. Here's the trick first.
The problem
Design a stack-like data structure that supports push and pop. push(x) adds x to the structure. pop() removes and returns the value with the highest current frequency. If several values have the same highest frequency, return the one pushed most recently among them. Process the operations in order and return the values produced by the pop operations. Function processFrequencyStack(operations: String[], values: int[]) → int[] Examples Example 1 operations = ["push","push","push","push","push","push","pop","pop","pop","pop"] values = [5,7,5,7,4,5,0,0,0,0] return = [5,7,5,4] The first pop returns 5 because it has frequency 3. The next two pops break frequency ties by recency, and the final pop returns 4. Example 2 operations = ["push","push","pop","pop"] values = [1,2,0,0] return = [2,1] Both values have frequency 1, so the more recently pushed value 2 is removed first. Constraints 1 <= operations.length <= 2 * 10^4 values.length == operations.length Each operation is either push or pop. 0 <= values[i] <= 10^9 for a push operation. Every pop operation is issued when the structure is nonempty. The value paired with a pop operation is ignored.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is a stack of stacks. Keep a map from value to its current frequency, and a second map from frequency to a stack of values that reached that frequency. Track maxFreq. On push(x), increment freq[x], then push x onto the stack for that new frequency and update maxFreq. On pop, take the top of the stack at maxFreq, decrement that value's frequency, and if that stack is now empty, decrement maxFreq. Recency is handled for free because each frequency stack is ordered by push time. The common pitfall is scanning for the max on every pop, which is O(n) and risks timing out near 2 * 10^4 operations. Another one: removing the value from lower frequency stacks. Don't. A value stays in each level it passed through, and that's correct. Ignore the value paired with pop. If the layout slips under pressure, StealthCoder is the hedge that keeps you moving during the live OA.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Maximum Frequency Stack cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as maximum frequency stack. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Maximum Frequency Stack FAQ
What's the trick to Maximum Frequency Stack?+
Use a map of value to frequency plus a map of frequency to a stack of values. Track the max frequency. Push adds the value to the stack for its new frequency. Pop takes from the top of the max frequency stack. Ties resolve by recency automatically.
How hard is this problem really?+
It's a hard-tagged design problem on paper, but the solution is short once you know it. The code is around 20 lines. The difficulty is spotting the stack-of-stacks idea, not writing it. Without that idea, people reach for heaps and get messy.
Can I use a heap instead?+
Yes. Store tuples of frequency, push order, and value in a max heap, with lazy handling of frequency. It works at O(log n) per operation, which fits 2 * 10^4 operations. The stack-of-stacks version is faster and simpler to reason about, so prefer it.
What edge cases should I test?+
Test all pushes of the same value, all distinct values, and alternating push and pop. Also test a pop that drops maxFreq to a lower level, like Example 1 reaching 4 at the end. Remember pop values in the input are ignored and pops are always valid.
How do I prepare in 48 hours for this Amazon OA?+
Write this one from memory twice, then do a couple of design-with-hash-map problems like LRU cache. Focus on pairing two maps to get O(1) operations. Practice returning the pop results in order as an int array, since the function signature expects that.