Maximum Frequency Stack
Updated
Problem statement
Process a sequence of operations on a frequency stack. A frequency stack supports the following operations:
pushadds an integer to the top of the stack.popremoves and returns an element whose current frequency in the stack is greatest. If several elements have the same greatest frequency, remove the one closest to the top of the stack.
The arrays operations and values have the same length. For index i:
- If
operations[i]is"push", pushvalues[i]. - If
operations[i]is"pop", ignorevalues[i], perform a pop, and append the removed value to the result.
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 removes 5 because it occurs three times. The next pop removes 7 because 5 and 7 are then tied and 7 is closer to the top. The final two pops return 5 and 4.
Example 2
operations = ["push","push","pop","pop"]values = [1,2,0,0]return = [2,1]Both values have frequency 1, so the first pop removes 2, which is closer to the top. The next pop removes 1.
Example 3
operations = ["push","push","push","pop"]values = [9,9,3,0]return = [9]Value 9 has frequency 2, which is greater than the frequency of 3.
Constraints
1 ≤ operations.length = values.length ≤ 20,000operations[i]is either"push"or"pop".0 ≤ values[i] ≤ 10^9whenoperations[i]is"push".- Every
popoperation occurs while the stack is non-empty.
More InMobi Phone Screen questions
- House Robber IPHONE SCREEN · Seen Aug 2026
- Word Break IIPHONE SCREEN · Seen Aug 2026
- Bus Station Seat AllotmentPHONE SCREEN · Seen Mar 2026
- Find Missing and Duplicate IntegerPHONE SCREEN · Seen Mar 2026
- Frequency Equals Card ValuePHONE SCREEN · Seen Mar 2026
- Frog Position After T SecondsPHONE SCREEN · Seen Mar 2026
- IPOPHONE SCREEN · Seen Mar 2026
- Evaluate DivisionPHONE SCREEN · Seen Dec 2025
- Compare Version NumbersPHONE SCREEN · Seen Feb 2022
- Count Array DerangementsPHONE SCREEN · Seen Feb 2022
Similar questions at other companies
- Maximum Frequency StackAmazon · PHONE SCREEN
- Nested Recipe Cart TransactionsDecagon · PHONE SCREENDecagon interview questions
- Maximum Frequency StackKalshi · PHONE SCREEN
- KV Store with Nested TransactionsApplied Intuition · PHONE SCREEN · ONSITE INTERVIEWApplied Intuition interview questions
- Layer History with Batch Undo and RedoFigma · OA · PHONE SCREENFigma interview questions
- In-Memory Database with Transactions and SavepointsNextdoor · PHONE SCREENNextdoor interview questions
- Key-Value Store with Nested TransactionsRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- Maximum Frequency StackBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
- Nested In-Memory Database TransactionsDRW · OADRW interview questions
- Nested Transaction Key-Value StorexAI · ONSITE INTERVIEWxAI interview questions