DIY: Maximum Frequency Stack
Solve the interview question "Maximum Frequency Stack" in this lesson.
We'll cover the following...
Problem statement
Design a stack-like data structure. We should be able to push elements to this data structure and pop elements with maximum frequency.
You have to implement the FreqStack
class:
class FreqStack
declare a frequency stackinitialize()
construct an empty frequency stackpush(data)
pushes an integerdata
onto the top of the stackpop() int
removes and returns the most frequent element in the stack
If there is a tie for the most frequent element, the most recently pushed element is removed and returned.
Note: If the stack is empty, return
-1
Access this course and 1400+ top-rated courses and projects.