DSA Visualizer
GRID

Spiral Traversal

Around the outside and inwards, clockwise, while four bounds close in. The classic interview question, one step at a time.

Around the outside and inwards, clockwise, until the bounds meet in the middle.

0
1
2
3
0
1
2
3
4
1
5
6
7
8
2
9
10
11
12
Visited
Press play to start
Variablestop=0bottom=2left=0right=3
Spiral order peels the grid like an onion: along the top, down the right, back along the bottom, up the left, then the same on the ring inside.
0
VisitedVisited
12
LeftRemaining
12
TotalTotal
1 / 14

Pseudocode

Spiral
1top = 0, bottom = rows-1, left = 0, right = cols-1
2while top <= bottom and left <= right:
3 for j in left .. right: visit(a[top][j]); top += 1
4 for i in top .. bottom: visit(a[i][right]); right -= 1
5 if top <= bottom: for j in right .. left: visit(a[bottom][j]); bottom -= 1
6 if left <= right: for i in bottom .. top: visit(a[i][left]); left += 1

Legend

Visiting nowNew directionVisited

Grid size

Want your own values? Edit this grid in the 2D Array Visualizer →

Tip: Space play/pause · ← → step

Learn spiral traversal

Spiral traversal peels the grid like an onion. It walks the outer ring clockwise, then shrinks the four bounds by one and walks the next ring, until top passes bottom or left passes right and nothing is left.

A traversal is just a rule for the order in which you visit cells. The grid does not change; only the path through it does. Compare it with row-major, column-major, snake, diagonal and boundary.

Every full traversal costs one step per cell, rows × cols, which is written O(rows · cols). Boundary is the exception: it visits only the outer ring.

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:

1 → 2 → 3 → 4 → 8 → 12 → 11 → 10 → 9 → 5 → 6 → 7

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.