Implement a fixed-capacity generic circular buffer
Company: Applied
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Design a data structure that stores generic elements `T` with the following properties:
- **FIFO** semantics (queue): `pop()` returns the oldest element.
- `push()` and `pop()` must be **O(1)**.
- The buffer has a fixed **capacity** known at class instantiation time, and **memory must be allocated at instantiation** (no per-operation allocation).
- When the buffer is full and you `push()` a new element, it **overwrites the oldest** element.
### Task
Implement a circular buffer class, e.g. `CircularBuffer<T, N>` where:
- `T` is the element type
- `N` is the fixed capacity (example: `N = 5`)
Implement at least:
- `push(value)`
- `pop()`
- `print()` (prints elements from oldest to newest)
### Define edge behavior
- What happens if `pop()` is called on an empty buffer?
- After overwriting, ensure ordering is still FIFO over the retained elements.
Quick Answer: This question evaluates knowledge of data structure design, generic types, in-place memory allocation, and constant-time FIFO operations required for implementing a fixed-capacity circular buffer.