Problem Challenge 3

We'll cover the following

Frequency Stack (hard) #

Design a class that simulates a Stack data structure, implementing the following two operations:

  1. push(int num): Pushes the number ‘num’ on the stack.
  2. pop(): Returns the most frequent number in the stack. If there is a tie, return the number which was pushed later.

Example:

After following push operations: push(1), push(2), push(3), push(2), push(1), push(2), push(5)
 
1. pop() should return 2, as it is the most frequent number
2. Next pop() should return 1
3. Next pop() should return 2

Try it yourself #

Try solving this question here:

Output

1.729s

-1 -1 -1

Mark as Completed
←    Back
Solution Review: Problem Challenge 2
Next    →
Solution Review: Problem Challenge 3