Head Meets Tail

Open Data Structures implements ArrayQueue with three fields: the backing array a, a head index j, and a count n. add(x) writes a[(j + n) % a.length] then increments n; remove() reads a[j], advances j, and decrements n.

An engineer drops n to save a word and tracks a tail index instead:

int a[8];
int head = 0, tail = 0;

bool empty(void) { return head == tail; }
void push(int x)  { a[tail] = x; tail = (tail + 1) % 8; }
int  pop(void)    { int x = a[head]; head = (head + 1) % 8; return x; }

Eight push calls run with no pop. What is the state?

A

The buffer works correctly but can never hold more than seven values, since the eighth push is rejected

B

All eight values are stored, but head == tail == 0, so empty() now reports true on a completely full buffer

C

The eighth push overwrites a[0], so the oldest value is silently lost

D

Nothing breaks: the modulo keeps every index in range, and head == tail is reachable only when the queue is empty