How spiral traversal works
Peel an onion one layer at a time. Spiral traversal walks the outer ring of the grid clockwise, then treats what is left as a smaller grid and walks its outer ring, and keeps going until the rings run out. The classic picture is the top row read left to right, then down the right edge, back along the bottom, up the left edge, and then the same again one step in.
The code does not need to know how many rings there are. It keeps four numbers, the bounds top, bottom, left and right, and moves each one inwards as soon as its edge has been walked. When top passes bottom or left passes right, there is nothing left inside and the loop stops.
It builds directly on boundary traversal: one ring of the spiral is exactly one boundary walk. Watch the four bound variables in the visualizer close in on the centre.
The loop shape
Start with top = 0, bottom = rows − 1, left = 0 and right = cols − 1. While the bounds have not crossed: walk row top from left to right, then top += 1; walk column right from the new top down to bottom, then right −= 1; walk row bottom from right to left, then bottom −= 1; walk column left from bottom up to top, then left += 1.
The last two walks are guarded by if top <= bottom and if left <= right. They matter only on the final ring, when a single row or column is left, but without them that row or column gets visited twice. Set the sliders to 1 row and watch the guards do their job.
A worked example: a 3 × 4 grid
With the cells numbered 1 to 12, the first ring is the boundary and the second ring is what remains in the middle:
Ring one: 1 2 3 4 down to 8 and 12, back through 11 10 9, up to 5. The bounds are now top 1, bottom 1, left 1, right 2, a single row. The top-row walk visits 6 and 7, top becomes 2 and passes bottom, and the guards skip the rest. Twelve cells, twelve visits, no repeats.
Where you'll meet it
- Interviews. Spiral Matrix is one of the most asked grid questions, and its variants (fill in spiral order, spiral from the centre, anticlockwise) all reuse the bounds.
- Image and scan processing. Reading a region from the outside in, or generating a spiral pattern of samples.
- Puzzles and games. Ulam's prime spiral, spiral-shaped level layouts, and snail-shell board numbering.
Mistakes beginners make
- Leaving out the two guards. The code passes on a square grid and repeats cells on a 1 × n or n × 1 remainder.
- Shrinking a bound before walking its edge. Each bound moves only after the edge it describes has been visited.
- Using a visited matrix by reflex. A grid of booleans works but costs extra memory; the four bounds do the same job in constant space.
Every full traversal on these pages took rows × columns steps. See how that growth compares with other algorithms: Big-O Playground →
Spiral traversal questions
What is spiral traversal of a matrix?
Visiting a 2D array in a clockwise spiral: the top row left to right, the right column top to bottom, the bottom row right to left, the left column bottom to top, and then the same on the smaller rectangle inside, until every cell has been visited once.
Why are the two if checks inside the loop needed?
After the top row and right column are done, the bounds shrink. On a grid with a single row left, top is now greater than bottom, and without the check the bottom row would be the row you just printed, read backwards. The same happens with the left column when a single column is left. The checks stop those repeats.
What is the time complexity of spiral traversal?
O(rows × cols). Every cell is visited exactly once, and the bounds bookkeeping adds only a constant amount of work per ring. The extra memory is O(1) if you print or stream the values, or O(rows × cols) if you collect them into a list.
Is this LeetCode 54, Spiral Matrix?
Yes. The four-bounds method here is the standard solution. LeetCode 59, Spiral Matrix II, is the reverse task, filling an empty n × n grid with 1 to n² in spiral order, and it uses exactly the same loop with a write instead of a read.