Design a stack that pops the most frequent element, breaking ties by recency
Quick Overview
Design a stack-like structure whose pop removes the most frequent value, with ties going to the copy closest to the top. It tests picking data structures that keep push and pop fast, handling the tie rule correctly, and keeping the highest count accurate after each pop.
Design a stack that pops the most frequent element, breaking ties by recency
Company: Microsoft
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: hard
Interview Round: Other
Design a stack-like data structure that lets you push integers onto the stack and pop the most frequent element from it.
If there is a tie for the most frequent element, the element closest to the stack's top is removed and returned.
Implement the following methods:
- `push(val: int)`: Pushes an integer `val` onto the top of the stack.
- `pop() -> int`: Removes and returns the most frequent element in the stack. If there is a tie, the element closest to the stack's top is removed and returned.
Each pop removes a single copy of the chosen value, the copy nearest the top. Other copies of that value stay in the stack.
Example 1:
```text
Input:
["FreqStack", "push", "push", "push", "push", "push", "push", "pop", "pop", "pop", "pop"]
[[], [4], [9], [4], [6], [9], [4], [], [], [], []]
Output: [null, null, null, null, null, null, null, 4, 9, 4, 6]
```
After the six pushes the stack is `[4, 9, 4, 6, 9, 4]` from bottom to top. The value 4 occurs three times, so the first pop returns 4. Then 4 and 9 both occur twice, and the 9 at the fifth position is closer to the top than any remaining 4, so the second pop returns 9. Now 4 is the only value that occurs twice, so it comes next. The stack is then `[4, 9, 6]`, every value occurs once, and 6 is on top.
Example 2:
```text
Input:
["FreqStack", "push", "push", "push", "pop", "push", "pop", "pop"]
[[], [1], [1], [2], [], [2], [], []]
Output: [null, null, null, null, 1, null, 2, 2]
```
After three pushes the stack is `[1, 1, 2]`. The value 1 occurs twice, so the pop returns 1 and leaves `[1, 2]`. Pushing 2 gives `[1, 2, 2]`, where 2 occurs twice, so the next pop returns 2 and leaves `[1, 2]`. Both values now occur once and 2 is closer to the top, so the last pop returns 2.
```hint What a pop has to find
Every pop needs two facts: the highest count among the values in the stack, and which value at that count sits highest. Think about what you can record during each push so that neither fact needs a scan.
```
```hint Group pushes by count
A value pushed three times passed through counts 1, 2 and 3 at different moments. Try keeping the push order separately for each count level.
```
```hint How far the maximum can fall
After a single pop, can the highest count in the stack drop by more than one?
```
### Constraints & Assumptions
- `0 <= val <= 10^9`
- At most `2 * 10^4` calls in total are made to `push` and `pop`.
- The stack is guaranteed to hold at least one element whenever `pop` is called.
### What a Strong Answer Covers
- The time cost of `push` and `pop`, and whether either one needs a scan or a heap over all values.
- How ties are resolved by position in the stack, and why the chosen structure produces that order.
- How the current highest count is kept correct after a pop removes the last value at that count.
- The memory used, given that a value pushed many times is stored once per copy.
- Working code traced on an example that contains a tie.
### Follow-up Questions
- How would you add `peek()`, which returns the value `pop()` would remove without changing the stack?
- What changes if `pop()` must remove every copy of the most frequent value at once?
- A priority queue keyed on count and push order also solves the problem. How does it compare in time and code complexity?
- How would you make the structure safe to use from several threads?
Overview: Design a stack-like structure whose pop removes the most frequent value, with ties going to the copy closest to the top. It tests picking data structures that keep push and pop fast, handling the tie rule correctly, and keeping the highest count accurate after each pop.
Design a stack-like data structure that lets you push integers onto the stack and pop the most frequent element from it.
If there is a tie for the most frequent element, the element closest to the stack's top is removed and returned.
Implement the following methods:
push(val: int)
: Pushes an integer
val
onto the top of the stack.
pop() -> int
: Removes and returns the most frequent element in the stack. If there is a tie, the element closest to the stack's top is removed and returned.
Each pop removes a single copy of the chosen value, the copy nearest the top. Other copies of that value stay in the stack.
After the six pushes the stack is [4, 9, 4, 6, 9, 4] from bottom to top. The value 4 occurs three times, so the first pop returns 4. Then 4 and 9 both occur twice, and the 9 at the fifth position is closer to the top than any remaining 4, so the second pop returns 9. Now 4 is the only value that occurs twice, so it comes next. The stack is then [4, 9, 6], every value occurs once, and 6 is on top.
After three pushes the stack is [1, 1, 2]. The value 1 occurs twice, so the pop returns 1 and leaves [1, 2]. Pushing 2 gives [1, 2, 2], where 2 occurs twice, so the next pop returns 2 and leaves [1, 2]. Both values now occur once and 2 is closer to the top, so the last pop returns 2.
Constraints & Assumptions
0 <= val <= 10^9
At most
2 * 10^4
calls in total are made to
push
and
pop
.
The stack is guaranteed to hold at least one element whenever
pop
is called.
What a Strong Answer Covers Guidance
The time cost of
push
and
pop
, and whether either one needs a scan or a heap over all values.
How ties are resolved by position in the stack, and why the chosen structure produces that order.
How the current highest count is kept correct after a pop removes the last value at that count.
The memory used, given that a value pushed many times is stored once per copy.
Working code traced on an example that contains a tie.
Follow-up Questions Guidance
How would you add
peek()
, which returns the value
pop()
would remove without changing the stack?
What changes if
pop()
must remove every copy of the most frequent value at once?
A priority queue keyed on count and push order also solves the problem. How does it compare in time and code complexity?
How would you make the structure safe to use from several threads?