DIY: Maximum Frequency Stack

Solve the interview question "Maximum Frequency Stack" in this lesson.

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 struct declare a frequency stack
  • Constructor() construct an empty frequency stack
  • Push(data int) pushes an integer data onto the top of the stack
  • Pop() 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.