Sub topic 2
Treating an array as a circle
This chapter assumes you know arrays with zero-based indexing, the FIFO queue idea (enqueue at the back, dequeue at the front), and basic Python. The modulo operator appears briefly here and gets its own chapter next.
The problem with a straight array
Suppose you store a queue in a fixed-size array. Enqueue writes at the back, and dequeue reads from the front. After a dequeue, the front slot is free, but the items behind it are still where they were.
You have two options, and both are poor:
- Shift every remaining item one slot toward the front after each dequeue. This costs O(n) per dequeue.
- Leave the items in place and move a front marker forward. The free slots at the start of the array are then wasted. The back marker eventually reaches the end of the array even though the array is mostly empty.
The first option is easy to see in Python:
queue = [10, 20, 30, 40]
front = queue.pop(0) # every remaining item shifts left
print(front, queue) # 10 [20, 30, 40]pop(0) returns the first item and moves the other items down one position. With millions of items, that copying adds up.
Joining the end to the start
A ring buffer takes the second option and fixes its flaw. When a marker moves past the last slot, it goes back to slot 0. The array is still one block of contiguous memory. You just treat the slot after the last one as the first one again.
Nothing in memory is actually circular. The circle exists only in how you calculate the next index. The following code walks around a five-slot array:
capacity = 5
i = 3
for _ in range(4):
print(i, end=" ")
i = (i + 1) % capacity # after slot 4 comes slot 0
print()This prints 3 4 0 1. The index moves forward by one each step. The % capacity keeps it inside the range 0 to 4. The chapter on wrapping explains why this works.
What the circle buys you
The ring gives you three properties at once:
- Fixed memory. You allocate the array once. It never grows, shrinks, or gets reallocated.
- No shifting. Items stay in their slots until they are read. Only the markers move.
- Reused space. A slot freed by a dequeue is available again as soon as the write position wraps around to it.
Together these make enqueue and dequeue O(1), meaning their cost does not depend on how many items are stored.
Logical order versus memory order
The queue's order is the order in which items were written, and that may not match their positions in memory. Say the buffer holds four slots, and the oldest item sits in slot 2. The next oldest is in slot 3, and the one after that is in slot 0, because the writer wrapped around.
Reading the array from index 0 to the end would then give you the items out of order. You always have to start at the oldest item and walk forward with wraparound.
slots = ["c", "d", "a", "b"] # oldest item is "a", at index 2
start = 2
ordered = [slots[(start + k) % len(slots)] for k in range(len(slots))]
print(ordered) # ['a', 'b', 'c', 'd']Here start plays the role of a marker for the oldest item. The next chapter defines that marker properly, along with its partner marker for the write position.
Check your understanding
Exercise
A ring buffer has capacity 6, and a marker is at index 5. What index does the marker move to after one step forward, and why is the array never shifted?
Show answer
It moves to index 0, because (5 + 1) % 6 is 0, so the position wraps to the start. The array never needs shifting because items stay in their slots and only the markers move. A freed slot is reused when the write marker wraps around to it.