FastPrepMaximum Frequency Stack

Maximum Frequency Stack

Kalshi logoKalshi● HardFULLTIMEPHONE SCREEN
Learn

Problem statement

Process operations on a frequency stack.

  • push adds its value.
  • pop removes and returns a value with the greatest current frequency. Among tied values, remove the one pushed most recently.

The value paired with a pop is ignored. Return the popped values in operation order.

Function

processFrequencyStack(operations: String[], values: int[]) → int[]

Examples

Example 1

operations = ["push","push","push","push","push","push","pop","pop","pop","pop"]values = [5,7,5,7,4,5,0,0,0,0]return = [5,7,5,4]

The first pop chooses frequency 3; later ties use push recency.

Example 2

operations = ["push","push","pop","pop"]values = [1,2,0,0]return = [2,1]

Both values initially have frequency one, so 2 leaves first.

Example 3

operations = ["push","push","push","pop"]values = [9,9,3,0]return = [9]

Frequency two beats the more recent frequency-one value.

Constraints

  • 1 <= operations.length == values.length <= 20000.
  • Each operation is push or pop.
  • 0 <= values[i] <= 10^9 for pushes.
  • Every pop occurs while the structure is nonempty.
See Kalshi hiring insights
public int[] processFrequencyStack(String[] operations, int[] values) {
    // Return values removed by pop operations.
}
operations["push","push","push","push","push","push","pop","pop","pop","pop"]
values[5,7,5,7,4,5,0,0,0,0]
expected[5,7,5,4]
Checking account…