Implement a Fixed-Size Stack Using an Array
Company: LTIMindtree
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: easy
Interview Round: Other
Design a stack data structure backed by a fixed-size array that follows the Last-In-First-Out (LIFO) principle.
The stack is initialized with a positive integer `capacity` that defines the maximum number of elements it can hold. The implementation must gracefully handle edge cases such as popping or peeking from an empty stack, and pushing onto a full stack.
When `pop` or `peek` is called on an empty stack, return `-1`. When `push` is called on a full stack, the operation should be ignored (no-op).
Implement the following methods:
- `push(x: int)`: Push element `x` onto the top of the stack. If the stack is full, the operation is ignored.
- `pop() -> int`: Remove and return the element at the top of the stack. Returns `-1` if the stack is empty.
- `peek() -> int`: Return the element at the top of the stack without removing it. Returns `-1` if the stack is empty.
Example 1:
```text
Input:
["ArrayStack", "push", "push", "peek", "pop", "pop", "pop"]
[[3], [10], [20], [], [], [], []]
Output: [null, null, null, 20, 20, 10, -1]
Explanation:
stack = ArrayStack(3) # capacity = 3, stack: []
stack.push(10) # stack: [10]
stack.push(20) # stack: [10, 20]
stack.peek() # 20 (top element)
stack.pop() # 20 (stack: [10])
stack.pop() # 10 (stack: [])
stack.pop() # -1 (stack is empty)
```
Example 2:
```text
Input:
["ArrayStack", "push", "push", "push", "push", "peek", "pop", "push", "peek"]
[[2], [1], [2], [3], [4], [], [], [5], []]
Output: [null, null, null, null, null, 2, 2, null, 5]
Explanation:
stack = ArrayStack(2) # capacity = 2, stack: []
stack.push(1) # stack: [1]
stack.push(2) # stack: [1, 2]
stack.push(3) # ignored (full), stack: [1, 2]
stack.push(4) # ignored (full), stack: [1, 2]
stack.peek() # 2 (top element)
stack.pop() # 2 (stack: [1])
stack.push(5) # stack: [1, 5]
stack.peek() # 5 (new top)
```
```hint Track the top
Besides the array, you need one integer. Decide exactly what it points at, then work out which of its values mean "empty" and which mean "full" before any method touches the array.
```
### Constraints & Assumptions
- `1 <= capacity <= 10⁴`
- At most `10⁵` calls will be made to `push`, `pop`, and `peek`.
- `-10⁹ <= x <= 10⁹` for pushed values.
### What a Strong Answer Covers
- What the index variable means, and how all three methods keep it consistent.
- How the full and empty states are detected before any array access.
- The time cost of each operation and the total space used, with the array allocated once.
- The behavior on the example sequences, including ignored pushes and a pop on an empty stack.
### Follow-up Questions
- Pushed values range from `-10⁹` to `10⁹`, so `-1` can be stored. How would you change the API so a caller can tell an empty stack apart from a stored `-1`?
- How would you make the stack grow when it fills up instead of ignoring pushes, and what is the amortized cost of `push` then?
- How would you fit two stacks into one fixed-size array so that neither overflows while free slots remain?
- What goes wrong if several threads call `push` and `pop` on the same instance, and how would you fix it?
Overview: Implement a stack on top of a fixed-size array with push, pop and peek, returning -1 when the stack is empty and ignoring pushes when it is full. It tests index bookkeeping, off-by-one handling at the full and empty boundaries, and constant-time operations.