Learn the vocabulary of a fixed-size buffer that wraps writes back around to its first slot.
0 / 5 completed
1 / 5
At standup, a dev mentions a fixed-size buffer where writing past the last slot simply wraps back around to the first slot, reusing the same block of memory indefinitely instead of growing or shifting elements. What is this structure called?
A circular buffer, also called a ring buffer, is a fixed-size buffer where writing past the last slot simply wraps back around to the first slot, reusing the exact same fixed block of memory indefinitely rather than growing its capacity or shifting existing elements to make room. A dynamic array instead grows by allocating a larger underlying block once it fills up, which is a fundamentally different strategy. This wrap-around behavior is exactly what makes a circular buffer well suited for a fixed-size streaming workload, like an audio buffer or a bounded log of recent events, where old data is meant to be overwritten rather than retained forever.
2 / 5
During a design review, the team tracks separate read and write positions that each wrap back to the start once they reach the buffer's end, specifically so the buffer never needs to shift its existing elements to make room for a new one. Which capability does this wrap-around tracking provide?
Tracking separate read and write positions that wrap around provides constant-time writes and reads regardless of how full the buffer currently is, since a new element always simply overwrites the next slot in place, with the write position advancing and wrapping as needed, instead of requiring every existing element to shift over to make room. Shifting every existing element on every single write would instead cost time proportional to how many elements the buffer currently holds, which gets slower as the buffer fills. This constant-time-regardless-of-fill-level property is exactly why a circular buffer is chosen over a plain array with manual shifting for a workload that continuously writes and reads in a fixed-size window.
3 / 5
In a code review, a dev notices a circular buffer's write operation never checks whether the write position is about to catch up to and pass the read position, only checking against the buffer's raw physical end. What does this represent?
This is a missing overwrite check, since a circular buffer's write position can wrap around and catch up to a read position that hasn't advanced far enough yet, and without an explicit check for that condition, the write will silently overwrite data the reader hasn't actually consumed, corrupting the stream from the reader's point of view. A cache eviction policy is an unrelated concept about discarded cache entries. This is exactly the kind of subtle correctness bug a reviewer needs to catch in a circular buffer implementation, since checking only against the buffer's raw physical end misses the far more important full-versus-empty distinction between the read and write positions.
4 / 5
An incident report shows a streaming service occasionally delivered corrupted data to slow consumers, because its circular buffer's write operation only checked against the buffer's raw physical end and never checked whether a fast producer's write position had caught up to and overwritten data a slow reader hadn't consumed yet. What practice would prevent this?
Adding an explicit full-buffer check comparing the write position against the current read position lets the buffer detect exactly when a fast producer is about to catch up to a slow reader, so it can block the write, drop the new data intentionally, or signal an overflow condition, instead of silently overwriting data the reader hasn't consumed yet, which is exactly the fix for the corrupted-data incident described here. Continuing to check only against the buffer's raw physical end, with no comparison against the read position, is exactly what let a fast producer silently corrupt unread data for a slow consumer. This read-versus-write-position overflow check is a required part of any correct circular buffer implementation used between a producer and a consumer running at different speeds.
5 / 5
During a PR review, a teammate asks why the team tracks both a read position and a write position in the circular buffer instead of just tracking a single position and trusting the fixed buffer size to prevent any overwrite issue automatically. What is the reasoning?
The buffer's fixed size alone says nothing about how far behind the reader currently is relative to the writer, so without explicitly comparing the write position against the actual read position, a producer running faster than its consumer can silently wrap all the way back around and overwrite data the reader hasn't gotten to yet, corrupting the stream without any indication anything went wrong. Tracking both positions separately is what lets the buffer actually detect this exact condition and respond to it, whether by blocking, dropping, or signaling overflow. The tradeoff is the small extra bookkeeping of maintaining and comparing two positions instead of one, which is a minor cost for correctness between a producer and consumer that can run at different speeds.