3.2.7 · D3Linear Data Structures

Worked examples — Queue — FIFO semantics, enqueue - dequeue, circular array implementation

3,584 words16 min readBack to topic

You have met the parent queue note and its rules. Now we hunt down every situation a circular-array queue can face and solve each one by hand, index by index. The goal: after this page, no queue scenario can surprise you.

Before we start, one reminder of the machine we are driving. A circular queue is described by exactly three numbers plus the array:


The scenario matrix

Every queue exercise is really one of these cells. We will hit all of them.

# Case class What makes it tricky Example
A Plain FIFO, no wrap baseline: does order come out right? Ex 1
B Rear wraps past the end rear falls off, must reappear at 0 Ex 2
C Front wraps past the end front itself crosses the seam Ex 3
D Fill to full, then reject isFull boundary, overflow Ex 4
E Drain to empty, then reject isEmpty boundary, underflow Ex 5
F Full vs empty ambiguity why front==rear is not enough Ex 6
G Degenerate capacity smallest possible ring Ex 7
H Real-world word problem BFS-style level processing Ex 8
I Exam twist: reconstruct state given a snapshot, find front/count Ex 9

Cells A–G are the mechanical cases. H connects to Breadth-First Search (BFS). I is the kind of gotcha exams love. (Capacity is the invalid case flagged above — no worked example, because no operation is defined.)

Throughout, I draw the array as a ring of chairs so the wrap is visible. Filled chairs are burnt orange, the front chair has a teal marker, the rear (next-free) chair has a plum marker.


Case A — plain FIFO, no wrap


Case B — the rear wraps

This is the reason circular queues exist. Watch rear fall off the right edge and land at 0.


Case C — the front wraps

Case B wrapped the rear. Now the front itself crosses the seam during a dequeue.


Case D — fill to full, then reject


Case E — drain to empty, then reject


Case F — full vs empty ambiguity

Cases D and E both ended with front == rear (both equalled 0). One was full, one was empty. That is the whole point of tracking count.


Case G — the degenerate ring,

Every data structure should be tested at its smallest legal size (recall ; is invalid). A one-chair ring is the extreme.


Case H — real-world word problem (BFS level count)


Case I — exam twist: reconstruct the hidden state

Exams love to give you a physical snapshot and ask for the invisible front/count.


Recall Feynman recap: the one rule behind every case

All nine cases obey a single sentence: the queue occupies count consecutive chairs on the ring, starting at front, and wrapping with mod N. Front chair leaves on dequeue; the chair just past the last occupied one receives on enqueue. If count hits N you are full (enqueue throws OverflowError); if it hits 0 you are empty (dequeue throws IndexError). Memorise the ring picture, not the cases.


Active Recall

In Example 2 (, front=2, count=2), what index does enqueue(C) write to?
rear=(2+2)%4=0, so arr[0]=C.
In Example 4, at what count does enqueue start rejecting, and which exception does it throw?
At countN3; it throws OverflowError (queue full).
Which exception does dequeue throw on an empty queue, and why?
IndexError — count==0 means the front slot has no valid element to read.
Why can't front==rear distinguish full from empty (Example 6)?
Because with count=0 and count=N both give rear=(front+count)%N=front; identical pointers, different meanings.
What is the smallest legal capacity, and what breaks at ?
; at there are no slots and is undefined (division by zero), so the queue is invalid.
On a ring with (Example 7), what is front always equal to?
0, since for every integer .
In the BFS run (Example 8), what processing order does FIFO produce?
R, P, Q, S — exact level order (breadth-first).
Given front=4, count=3, N=6 (Example 9), what is rear?
(4+3)%6 = 7%6 = 1.