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:

  • class FreqStack declare a frequency stack
  • __init__() 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.