Design a stack that returns its minimum and maximum in constant time
Quick Overview
Design a stack with push, pop and top that can also return its current minimum and maximum element in constant time. It tests how to keep auxiliary state at each stack level so a pop restores the earlier extremes, how to handle duplicate values, and the time and space cost of the design.
Design a stack that returns its minimum and maximum in constant time
Company: Twilio
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: easy
Interview Round: Other
Design a stack that supports `push`, `pop` and `top` operations, and can retrieve both the **minimum** and **maximum** elements in constant time.
All five operations (`push`, `pop`, `top`, `getMin`, `getMax`) must run in `O(1)` time.
Implement the `MinMaxStack` class with the following methods:
- `push(val: int) -> None` pushes the element `val` onto the stack.
- `pop() -> None` removes the element on the top of the stack.
- `top() -> int` returns the element on the top of the stack.
- `getMin() -> int` returns the minimum element currently in the stack.
- `getMax() -> int` returns the maximum element currently in the stack.
Example 1:
```text
Input:
["MinMaxStack", "push", "push", "push", "getMin", "getMax", "pop", "top", "getMin", "getMax"]
[[], [-2], [0], [-3], [], [], [], [], [], []]
Output: [null, null, null, null, -3, 0, null, 0, -2, 0]
stack = MinMaxStack() # empty stack
stack.push(-2) # stack: [-2]
stack.push(0) # stack: [-2, 0]
stack.push(-3) # stack: [-2, 0, -3]
stack.getMin() # -3
stack.getMax() # 0
stack.pop() # removes -3, stack: [-2, 0]
stack.top() # 0
stack.getMin() # -2 (new minimum)
stack.getMax() # 0
```
After -3 is popped, the minimum falls back to -2, the smallest value still on the stack.
Example 2:
```text
Input:
["MinMaxStack", "push", "push", "getMax", "getMin", "pop", "push", "top", "getMin", "getMax"]
[[], [5], [1], [], [], [], [3], [], [], []]
Output: [null, null, null, 5, 1, null, null, 3, 3, 5]
stack = MinMaxStack() # empty stack
stack.push(5) # stack: [5]
stack.push(1) # stack: [5, 1]
stack.getMax() # 5
stack.getMin() # 1
stack.pop() # removes 1, stack: [5]
stack.push(3) # stack: [5, 3]
stack.top() # 3
stack.getMin() # 3
stack.getMax() # 5
```
Once 1 is gone and 3 is pushed, the stack holds 5 and 3, so the minimum is 3 and the maximum is 5.
```hint What each level can remember
A pop returns the stack to exactly the state it was in before the matching push. Think about what you could record at push time so that the earlier state's minimum and maximum are still on hand afterwards.
```
### Constraints & Assumptions
- `-10^9 <= val <= 10^9`
- At most `10^4` calls in total will be made to `push`, `pop`, `top`, `getMin` and `getMax`.
- `pop`, `top`, `getMin` and `getMax` are always called on a non-empty stack.
### Clarifying Questions to Ask
- Is extra memory proportional to the number of elements acceptable, or is there a tighter memory budget?
- Does `O(1)` have to be worst case, or is amortized `O(1)` from a growable array fine?
- Can the same value be pushed more than once?
### What a Strong Answer Covers
- Why a single running minimum and maximum variable breaks once a pop removes the current extreme.
- Storing the running extremes per stack level (one tuple per entry or parallel stacks) and computing them from the previous top on push.
- The first push on an empty stack and repeated values equal to the current minimum or maximum.
- `O(1)` time for every operation and the extra space the design uses.
### Follow-up Questions
- How would you reduce the extra memory when the minimum and maximum rarely change?
- How would you add `popMax()`, which removes and returns the largest element, and what would it cost?
- How would you get constant-time minimum and maximum for a queue instead of a stack?
Overview: Design a stack with push, pop and top that can also return its current minimum and maximum element in constant time. It tests how to keep auxiliary state at each stack level so a pop restores the earlier extremes, how to handle duplicate values, and the time and space cost of the design.