data-structures · intermediate · ~15 min

Queue length in a ring

Compute occupancy in a circular buffer.

Challenge

Implement int ring_count(int head, int tail, int cap) returning the number of items between head (front) and tail (next free slot) in a ring buffer, where items wrap around. 0 <= head,tail < cap.

Starter code

int ring_count(int head, int tail, int cap) {
    /* TODO */
    return 0;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.