Design a circular buffer with overwrite-on-full, pop and an oldest-to-newest snapshot
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: easy
Interview Round: Other
Design a fixed-capacity circular buffer (also known as a ring buffer) of size `capacity`. The buffer stores elements in insertion order and, when full, overwrites the oldest element on the next push.
The buffer must support adding a new element, removing and returning the oldest element, and retrieving a snapshot of all current elements ordered from oldest to newest.
If `pop` is called on an empty buffer, return `-1`.
The constructor `CircularBuffer(capacity)` creates an empty buffer. Implement the following methods:
- `push(value: int)`: Append `value` to the buffer. If the buffer is full, overwrite the oldest element.
- `pop() -> int`: Remove and return the oldest element. Return `-1` if the buffer is empty.
- `snapshot() -> List[int]`: Return a list of all current elements in order from oldest to newest.
Example 1:
```text
Input:
["CircularBuffer", "push", "push", "push", "snapshot", "push", "snapshot", "pop", "snapshot", "pop", "pop", "pop"]
[[3], [1], [2], [3], [], [4], [], [], [], [], [], []]
Output: [null, null, null, null, [1, 2, 3], null, [2, 3, 4], 2, [3, 4], 3, 4, -1]
Explanation:
buf = CircularBuffer(3) # capacity = 3
buf.push(1) # buffer: [1]
buf.push(2) # buffer: [1, 2]
buf.push(3) # buffer: [1, 2, 3] (full)
buf.snapshot() # [1, 2, 3]
buf.push(4) # overwrites the oldest (1), buffer: [2, 3, 4]
buf.snapshot() # [2, 3, 4]
buf.pop() # 2 (removes the oldest)
buf.snapshot() # [3, 4]
buf.pop() # 3
buf.pop() # 4
buf.pop() # -1 (buffer is empty)
```
Example 2:
```text
Input:
["CircularBuffer", "pop", "push", "snapshot", "pop", "snapshot"]
[[2], [], [10], [], [], []]
Output: [null, -1, null, [10], 10, []]
Explanation:
buf = CircularBuffer(2) # capacity = 2
buf.pop() # -1 (empty buffer)
buf.push(10) # buffer: [10]
buf.snapshot() # [10]
buf.pop() # 10
buf.snapshot() # [] (buffer is empty)
```
```hint Full versus empty
If you keep only a read index and a write index, a full buffer and an empty buffer leave them in the same position. Decide what extra state tells the two apart.
```
### Constraints & Assumptions
- `1 <= capacity <= 10^4`
- At most `10^5` calls are made across `push`, `pop` and `snapshot`
- `-10^9 <= value <= 10^9`
### Clarifying Questions to Ask
- Values may be `-1`, so how should a caller tell a popped `-1` from an empty buffer? Is the sentinel fixed by the interface?
- Should `snapshot` return an independent copy, or may it share storage with the buffer?
- Will the buffer be used from more than one thread, for example one producer and one consumer?
### What a Strong Answer Covers
- A storage layout that allocates once at construction and keeps `push` and `pop` at O(1).
- How the full and empty states are told apart, and what a push into a full buffer does to the read position.
- Wraparound in `snapshot`, so the order stays oldest to newest after the write position has wrapped past the end of the array.
- The empty-buffer cases for `pop` and `snapshot`, and the ambiguity of the `-1` sentinel.
- The time and space cost of each operation.
### Follow-up Questions
- How would you make the buffer safe for one producer thread and one consumer thread without a lock?
- How would you change the API so that a full buffer rejects pushes instead of overwriting, and when is each policy the right one?
- How would you add `peek()` for the oldest element and `get(i)` for the i-th oldest element, both in O(1)?
- If the capacity were always a power of two, what could replace the modulo, and why do some implementations let the indices grow without wrapping them?
Overview: Design a fixed-capacity circular buffer that overwrites its oldest element when full, with push, pop and an oldest-to-newest snapshot. It tests index arithmetic with wraparound, telling a full buffer from an empty one, and constant-time queue operations on a preallocated array.