I knew the modular arithmetic part well enough but fumbled explaining how to distinguish empty vs full state.
Start by clarifying the requirements and constraints, then describe the array-based implementation with head and tail indices, emphasizing the modulo arithmetic for wrap-around. Explain the full/empty distinction using either a size counter or a reserved slot, and conclude with O(1) time and O(n) space complexity.
Pro tip: Mention the trade-off between using a size counter (simpler, uses extra space) and a reserved slot (no extra space, but capacity effectively n-1). This shows you consider practical constraints and can adapt to interviewer preferences.
Ask if the queue is fixed-capacity, if it's single-threaded, and if the capacity is known upfront. Confirm that all operations must be O(1).
Use an array of size capacity, and maintain head (front index) and tail (next insertion index) indices. Optionally, maintain a size variable or use a reserved slot to distinguish full from empty.
For enqueue: check if full, place element at tail, update tail = (tail + 1) % capacity. For dequeue: check if empty, retrieve element at head, update head = (head + 1) % capacity.
If using a size counter: empty when size == 0, full when size == capacity. If using a reserved slot: empty when head == tail, full when (tail + 1) % capacity == head.
All operations are O(1) time. Space is O(capacity). Discuss edge cases: enqueue to full queue, dequeue from empty queue, and wrap-around behavior.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.