data-structures · beginner · ~15 min

Ring-buffer index

Use modular arithmetic for wrap-around indexing.

Challenge

A queue backed by a ring buffer of capacity cap wraps around. Implement int ring_index(int start, int k, int cap) returning the buffer index k positions after start (0 <= start < cap, k >= 0).

Starter code

int ring_index(int start, int k, int cap) {
    /* TODO */
    return 0;
}

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