Implement k Stacks Sharing One Array of Size n with O(1) Push and Pop
Quick Overview
Design k stacks that share one array of size n, with push and pop in constant time and no fixed partition of the slots. It tests index-based linked lists, free-slot management, and careful pointer updates in a low-level design setting.
Implement k Stacks Sharing One Array of Size n with O(1) Push and Pop
Company: Texas Instruments
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a data structure that implements **k stacks** using a single underlying array of size `n`. The implementation must support `push(x, stackNum)` and `pop(stackNum)` operations, each running in `O(1)` time.
The structure must use **all** `n` slots across all `k` stacks. The array is not pre-partitioned: any stack can grow to consume free slots wherever they exist in the array.
If `pop` is called on an empty stack, return `-1`. If `push` is called when the array is completely full, return `-1` (and do not insert). Otherwise `push` returns `0` on success.
Implement the following methods:
- `NStacks(k: int, n: int)`: create `k` empty stacks that share one array of `n` slots.
- `push(x: int, stackNum: int) -> int`: push value `x` onto stack `stackNum`. Returns `0` on success, or `-1` if the underlying array is full.
- `pop(stackNum: int) -> int`: pop and return the top value from stack `stackNum`. Returns `-1` if that stack is empty.
Example 1, with three stacks sharing five slots:
```text
Input:
["NStacks", "push", "push", "push", "push", "pop", "pop", "pop", "pop"]
[[3, 5], [10, 0], [20, 1], [30, 0], [40, 2], [0], [0], [1], [2]]
Output: [null, 0, 0, 0, 0, 30, 10, 20, 40]
```
```python
stacks = NStacks(3, 5) # k = 3 stacks sharing an array of size n = 5
stacks.push(10, 0) # -> 0 (stack 0: [10])
stacks.push(20, 1) # -> 0 (stack 1: [20])
stacks.push(30, 0) # -> 0 (stack 0: [10, 30])
stacks.push(40, 2) # -> 0 (stack 2: [40])
stacks.pop(0) # -> 30 (stack 0: [10])
stacks.pop(0) # -> 10 (stack 0: [])
stacks.pop(1) # -> 20 (stack 1: [])
stacks.pop(2) # -> 40 (stack 2: [])
```
Example 2, where the array fills up:
```text
Input:
["NStacks", "push", "push", "push", "push", "pop", "pop"]
[[2, 3], [1, 0], [2, 0], [3, 1], [4, 0], [1], [0]]
Output: [null, 0, 0, 0, -1, 3, 2]
```
```python
stacks = NStacks(2, 3) # k = 2 stacks sharing an array of size n = 3
stacks.push(1, 0) # -> 0 (stack 0: [1])
stacks.push(2, 0) # -> 0 (stack 0: [1, 2])
stacks.push(3, 1) # -> 0 (stack 1: [3])
stacks.push(4, 0) # -> -1 (array full, push rejected)
stacks.pop(1) # -> 3 (stack 1: [])
stacks.pop(0) # -> 2 (stack 0: [1])
```
```hint Scattered elements
Once any stack can sit in any slot, a stack's elements end up spread across the array. Ask what each slot would need to store so the stack can find the element below its top after a pop.
```
```hint Reusing slots
A popped slot has to become available to every stack again, and a push has to find some free slot without scanning. Think about which structure hands you an arbitrary free item in constant time.
```
### Constraints & Assumptions
- `1 <= n <= 10^5`
- `1 <= k <= n`
- `0 <= stackNum < k`
- `-10^9 <= x <= 10^9`
- At most `10^5` calls in total to `push` and `pop`.
### Clarifying Questions to Ask
- Besides the array of values, may I keep auxiliary index arrays of size `n` and `k`, or must all bookkeeping live inside the one array?
- A pushed value can itself be `-1`. Is it acceptable that a `pop` returning `-1` is ambiguous in that case, or should I expose an emptiness check as well?
- Are `push` and `pop` the only operations needed, or should I also support `peek` and `isEmpty`?
### What a Strong Answer Covers
- Why fixed blocks of `n / k` slots, or shifting elements on overflow, break either the "use all n slots" rule or the `O(1)` bound.
- How a stack finds its next element down when its slots are scattered through the array.
- How freed slots are tracked and handed out again in `O(1)`, and how a full array is detected.
- The exact index updates in `push` and `pop`, in an order that never loses a link.
- Time and space cost, plus the edge cases: popping an empty stack, pushing into a full array, and one stack using every slot.
### Follow-up Questions
- How would you add `peek(stackNum)` and `isEmpty(stackNum)`, and what do they cost?
- How would you implement `k` queues in one array with the same `O(1)` bounds?
- Can you drop the extra index array of size `n`? What do you give up for `k = 2` and for larger `k`?
- How would you make the structure safe when several threads push and pop at once?
Overview: Design k stacks that share one array of size n, with push and pop in constant time and no fixed partition of the slots. It tests index-based linked lists, free-slot management, and careful pointer updates in a low-level design setting.
Design a data structure that implements k stacks using a single underlying array of size n. The implementation must support push(x, stackNum) and pop(stackNum) operations, each running in O(1) time.
The structure must use alln slots across all k stacks. The array is not pre-partitioned: any stack can grow to consume free slots wherever they exist in the array.
If pop is called on an empty stack, return -1. If push is called when the array is completely full, return -1 (and do not insert). Otherwise push returns 0 on success.
Implement the following methods:
NStacks(k: int, n: int)
: create
k
empty stacks that share one array of
n
slots.
push(x: int, stackNum: int) -> int
: push value
x
onto stack
stackNum
. Returns
0
on success, or
-1
if the underlying array is full.
pop(stackNum: int) -> int
: pop and return the top value from stack
stackNum
. Returns
-1
if that stack is empty.