Skip to content
DeepForgeby svx
All problems
HardData Structuresds-124

Maximum Frequency Stack

Problem

Simulate a maximum frequency stack.

operations contains "push" (values[i]) or "pop". push records None. pop removes and returns the most frequently pushed value; ties are broken by recency, so the most recently pushed among the most frequent wins. Frequency counts update on every pop.

Return the list of recorded results.

Hint

Keep one list per frequency level; the top of the highest non-empty list is the value to pop.

Test cases

case 1
in: [["push","push","push","push","push","pop","pop","pop","pop"],[5,7,5,7,4,0,0,0,0]]
expected: [null,null,null,null,null,7,5,4,7]
case 2
in: [["push","pop"],[1,0]]
expected: [null,1]
case 3
in: [["push","push","pop","pop"],[2,2,0,0]]
expected: [null,null,2,2]
case 4
in: [["push","push","push","pop"],[1,2,3,0]]
expected: [null,null,null,3]
Starter codePython
def max_freq_stack(operations, values):
    # Your code here
    pass

4 test cases · run your code against them in the editor