Implement a row of capacity-limited stacks with leftmost push, rightmost pop and pop by index
Quick Overview
Design a class that manages an unbounded row of fixed-capacity stacks, pushing to the leftmost stack with room and popping from the rightmost non-empty stack or from any stack by index. It tests picking a structure for the smallest free index, lazy cleanup of stale entries, and amortized complexity analysis.
Implement a row of capacity-limited stacks with leftmost push, rightmost pop and pop by index
Company: ByteDance
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: hard
Interview Round: Other
You have an infinite row of stacks, numbered from `0` left to right. Every stack has the same maximum `capacity`.
Implement the `DinnerPlates` class with these methods:
- `DinnerPlates(capacity: int)` creates the row with the given capacity per stack.
- `push(val: int) -> None` pushes `val` onto the leftmost stack that is not full.
- `pop() -> int` pops and returns the value at the top of the rightmost non-empty stack, or `-1` if all stacks are empty.
- `popAtStack(index: int) -> int` pops and returns the value at the top of the stack at `index`, or `-1` if that stack is empty or out of range.
Example 1, with capacity 2:
```text
Input:
["DinnerPlates", "push", "push", "push", "popAtStack", "push", "push", "popAtStack", "popAtStack", "popAtStack", "pop", "pop", "pop"]
[[2], [1], [2], [3], [0], [4], [5], [1], [1], [1], [], [], []]
Output:
[null, null, null, null, 2, null, null, 5, 3, -1, 4, 1, -1]
```
After three pushes the stacks are `[[1, 2], [3]]`. `popAtStack(0)` returns 2 and frees a slot in stack 0, so `push(4)` goes there and `push(5)` fills stack 1. The three pops at index 1 return 5, then 3, then `-1` because stack 1 is empty by then. `pop()` passes over the empty stack 1 and returns 4, then 1, then `-1`.
Example 2, with capacity 1:
```text
Input:
["DinnerPlates", "push", "push", "pop", "pop"]
[[1], [1], [2], [], []]
Output:
[null, null, null, 2, 1]
```
With capacity 1 every push opens a new stack, giving `[[1], [2]]`. The first `pop()` takes 2 from stack 1 and the second takes 1 from stack 0.
```hint Finding the leftmost free stack
Scanning from stack 0 on every push is linear in the number of stacks. Keep a separate structure that can hand you the smallest index of a stack with room, and think about when an index has to enter or leave it.
```
```hint Gaps left by popAtStack
Indexed pops can empty stacks in the middle and at the end of the row. Work out which empty stacks must keep their index and which can be cleaned up the next time you look at them.
```
### Constraints & Assumptions
- `1 <= capacity <= 2 * 10^4`
- `1 <= val <= 2 * 10^4`
- `0 <= index <= 10^5`
- At most `2 * 10^5` calls in total are made to `push`, `pop` and `popAtStack`.
- Values are positive, so `-1` never collides with a stored value.
### What a Strong Answer Covers
- A structure that returns the leftmost stack with free space faster than a linear scan, and how it stays correct after pushes, pops and indexed pops.
- How `pop` finds the rightmost non-empty stack when indexed pops have emptied stacks at the end of the row.
- Why an empty stack in the middle of the row has to keep its index.
- Correct `-1` results for empty and out-of-range stacks, and the capacity 1 case.
- The time and space cost per operation, with the amortized argument spelled out.
### Follow-up Questions
- How would you keep the free-index structure from holding more than one entry per stack, and what does that change in the complexity?
- Indexed pops can leave long runs of empty stacks in the middle. How would you store the stacks so memory tracks the number of plates instead of the highest index ever used?
- How would you make the class safe to call from several threads at once?
Overview: Design a class that manages an unbounded row of fixed-capacity stacks, pushing to the leftmost stack with room and popping from the rightmost non-empty stack or from any stack by index. It tests picking a structure for the smallest free index, lazy cleanup of stale entries, and amortized complexity analysis.
After three pushes the stacks are [[1, 2], [3]]. popAtStack(0) returns 2 and frees a slot in stack 0, so push(4) goes there and push(5) fills stack 1. The three pops at index 1 return 5, then 3, then -1 because stack 1 is empty by then. pop() passes over the empty stack 1 and returns 4, then 1, then -1.
With capacity 1 every push opens a new stack, giving [[1], [2]]. The first pop() takes 2 from stack 1 and the second takes 1 from stack 0.
Constraints & Assumptions
1 <= capacity <= 2 * 10^4
1 <= val <= 2 * 10^4
0 <= index <= 10^5
At most
2 * 10^5
calls in total are made to
push
,
pop
and
popAtStack
.
Values are positive, so
-1
never collides with a stored value.
What a Strong Answer Covers Guidance
A structure that returns the leftmost stack with free space faster than a linear scan, and how it stays correct after pushes, pops and indexed pops.
How
pop
finds the rightmost non-empty stack when indexed pops have emptied stacks at the end of the row.
Why an empty stack in the middle of the row has to keep its index.
Correct
-1
results for empty and out-of-range stacks, and the capacity 1 case.
The time and space cost per operation, with the amortized argument spelled out.
Follow-up Questions Guidance
How would you keep the free-index structure from holding more than one entry per stack, and what does that change in the complexity?
Indexed pops can leave long runs of empty stacks in the middle. How would you store the stacks so memory tracks the number of plates instead of the highest index ever used?
How would you make the class safe to call from several threads at once?