Theory

Watch: a stack and a ring, row by row

There is no code here yet. What is here is what happens to stacks and queues when you use them — one step at a time, with four items A B C D.

You write nothing in this step. Read the tables, and in the next step you will recognise the code at once.

How we count

Every row has a moved column — the same counter you will see in the code in the next step: how many elements were relocated. Push, Pop and moving an index do not raise it.

The stack

operation       contents (top on the right)  len     moved
push A          [A]                          1       0
push B          [A B]                        2       0
push C          [A B C]                      3       0
pop → C         [A B]                        2       0
pop → B         [A]                          1       0
pop → A         []                           0       0
pop (empty)     []                           0       0   → ok = false

Every operation touches only the top. A and B sit exactly where they were the whole time while C comes and goes.

Queue, way A: q = q[1:]

operation    q            len  cap   array underneath    moved
start        [A B C D]    4    4     [A B C D]           0
dequeue → A  [B C D]      3    3     [A B C D]           0
dequeue → B  [C D]        2    2     [A B C D]           0
dequeue → C  [D]          1    1     [A B C D]           0
dequeue → D  []           0    0     [A B C D]           0

Only the slice header changes: the pointer moves forward one slot, len and cap shrink. The array under it does not change — all four items are still in their slots.

Queue, way C: the ring buffer

Capacity is 4 slots. head says where to take from, tail where to put, n how many are in use. A dot · is an empty slot.

operation     buffer       head  tail  n   moved
start         [· · · ·]    0     0     0   0
enqueue A     [A · · ·]    0     1     1   0
enqueue B     [A B · ·]    0     2     2   0
enqueue C     [A B C ·]    0     3     3   0
enqueue D     [A B C D]    0     0     4   0     tail wrapped to 0
enqueue E     [A B C D]    0     0     4   0     FULL — rejected
dequeue → A   [· B C D]    1     0     3   0
dequeue → B   [· · C D]    2     0     2   0
enqueue E     [E · C D]    2     1     3   0     E landed in slot 0
enqueue F     [E F C D]    2     2     4   0
dequeue → C   [E F · D]    3     2     3   0
dequeue → D   [E F · ·]    0     2     2   0     head wrapped to 0
dequeue → E   [· F · ·]    1     2     1   0
dequeue → F   [· · · ·]    2     2     0   0
dequeue       [· · · ·]    2     2     0   0     EMPTY

The third way, B — where every dequeue shifts all remaining items one slot to the left — is deliberately not shown: you already wrote its number down in your guess, and the next step shows it.

Three observations

1. The moved column is zero everywhere. Neither the stack's Push/Pop, nor way A, nor the ring touches other elements. The difference between them is not here — so in the next step look at what way B does differently.

2. In way A the header changes, not the array. In the last row q is empty but [A B C D] is still underneath. Remember that row — it comes back.

3. In the ring, head and tail wrap around. After enqueue D both are 0 — exactly as at the very start, when the buffer was empty. Only n tells a full buffer from an empty one. Also note E ended up in slot 0 although it is the newest item: the index is computed as (tail + 1) % capacity.

What next

In the next step you write this as Go code: History, ways A and B, Ring — and run it on 100,000 items.