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:
FreqStack()
constructs an empty frequency stackpush(data int)
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
Level up your interview prep. Join Educative to access 80+ hands-on prep courses.