AlgoMaster Logo

Spiral Number Pattern

medium6 min readUpdated July 3, 2026

Understanding the Problem

Unlike the earlier row-by-row patterns, the spiral winds around the edge of the grid and curls inward, like water draining around a square basin.

Trace the path for a 3 by 3 grid. The number 1 starts in the top-left corner. From there the count moves right across the top row to 3, turns and moves down the right column to 5, turns again and runs left across the bottom row to 7, then climbs up the left column to 8. That single loop traces the entire outer ring. Only the center is left, so 9 goes in the middle and the grid is full.

The difficulty is the turning, not the arithmetic. After each straight run the direction changes, and the region you can still write into shrinks, since the ring you just filled is now off limits. Getting it right depends on knowing when to turn and never stepping onto a cell already filled. Two clean ways to handle that follow.

Key Constraints:

  • 1 <= n <= 50n is at least 1, so there is always at least the single-cell grid [[1]]. The square shape means rows and columns share the same length n.
  • Numbers run 1 to n^2 → Every cell gets a distinct value, and the largest, n^2, is at most 2500, which sits well inside a 32-bit integer.
  • Clockwise from the top-left → The direction order is fixed: right, then down, then left, then up, repeating as the spiral tightens.

Approach 1: Shrinking Boundaries

Intuition

Picture four walls around the part of the grid still waiting to be filled: a top row, a bottom row, a left column, and a right column. The spiral sweeps across the top wall, down the right wall, back along the bottom wall, and up the left wall. That completes one ring.

Once a wall has been used, its cells are filled, so pull that wall inward by one: the top wall moves down, the right wall moves left, and so on. The walls keep closing in until they cross, at which point every cell has a number. A single counter, increasing from 1 to n^2, supplies the value for each cell.

Algorithm

  1. Create an n by n grid, and set top = 0, bottom = n - 1, left = 0, right = n - 1, with a counter starting at 1.
  2. While top <= bottom and left <= right, do the following four passes.
  3. Fill the top row from left to right, then move top down by one.
  4. Fill the right column from top to bottom, then move right left by one.
  5. If top <= bottom, fill the bottom row from right to left, then move bottom up by one.
  6. If left <= right, fill the left column from bottom to top, then move left right by one.
  7. Return the grid once the walls have crossed.

Example Walkthrough

Input:

3
n

The walls begin at top = 0, bottom = 2, left = 0, right = 2. The top row fills left to right with 1, 2, 3, then top becomes 1. The right column fills from row 1 down with 4 and 5, then right becomes 1. The bottom row fills right to left with 6 and 7, then bottom becomes 1. The left column fills from row 1 up with 8, then left becomes 1. Now top and left are both 1, so the only cell left is the center, which the next top-row pass fills with 9. The completed grid is:

0
1
2
0
1
2
3
1
8
9
4
2
7
6
5
result

Code

The four passes work, but they need careful bookkeeping, and the two guard checks exist only to avoid rewriting a row or column when the spiral narrows to a single line. There is a way to describe the same walk with one rule for turning instead of four separate passes.

Approach 2: Direction Vectors With Turn-on-Block

Intuition

Treat the spiral as one walker that keeps going straight until it has to turn. Instead of four passes, hold a single position and a single heading and step forward one cell at a time. The walker turns only when going straight would run off the grid or step onto a cell that already holds a number.

Store the four clockwise headings as changes in row and column. Right adds 1 to the column, down adds 1 to the row, left subtracts 1 from the column, and up subtracts 1 from the row. When the next step is blocked, rotate to the next heading in the cycle. Because the spiral is a single connected path, this rule alone reproduces the whole pattern.

Algorithm

  1. Create an n by n grid filled with a marker value of 0, meaning unfilled.
  2. Set the direction changes for right, down, left, and up in that order, and start the heading at right.
  3. Start at row 0, column 0, with a counter from 1 to n^2.
  4. For each value, write it into the current cell, then compute the next cell using the current heading.
  5. If the next cell is outside the grid or already filled, rotate to the next heading and recompute the next cell.
  6. Move to the next cell and repeat until all n^2 values are placed.
  7. Return the grid.

Example Walkthrough

Input:

3
n

The walker starts at the top-left heading right, placing 1, 2, 3 across the top row. The next step right would leave the grid, so it turns down and places 4, then 5. Stepping down again would leave the grid, so it turns left and places 6, then 7. Stepping left would leave the grid, so it turns up and places 8. Now stepping up would land on the already-filled 1, so it turns right and places 9 in the center. Every cell is filled, giving the same spiral:

0
1
2
0
1
2
3
1
8
9
4
2
7
6
5
result

Code