Spiral · Pattern

Anti-clockwise Spiral

Anti-clockwise Spiral

Spiral

Symbol pattern, solid style, size 5. Changing the rubric, style or size above re-shapes the loops — the structure stays identical while only the printed value or the cell filter changes.

Anti-clockwise Spiral · output

Symbol · Solid · size 5

max 25
     
     
     
     
     
1 / 12
Drawing… ready to start.
Python · Anti-clockwise Spiral · Python
    n = 5
m = [[0] * n for _ in range(n)]
k = 1
top, bottom, left, right = 0, n - 1, 0, n - 1
while top <= bottom and left <= right:
    for i in range(top, bottom + 1): m[i][left] = k; k += 1
    left += 1
    for j in range(left, right + 1): m[bottom][j] = k; k += 1
    bottom -= 1
    for i in range(bottom, top - 1, -1): m[i][right] = k; k += 1
    right -= 1
    for j in range(right, left - 1, -1): m[top][j] = k; k += 1
    top += 1
for row in m:
    for x in row:
        print(x, end='')
        print(' ', end='')
    print()

Behind the Scenes Execution

registers · line tracking · call stack · memory regions
PC0x4000SP0x7FE8FLAGSZF 0OPenter function
Call Stack
main()@0x7FD0
n = 5i = -1j = -1count = 0
Memory Map
AddressVariableValue
Stack (4)
0x200n5
0x204i-1
0x208j-1
0x20Ccount0
String Pool (static) (2)
0x400000pool["*"]*
0x400008pool["\n"]\n
Heap (5)
0x3000grid[0]
0x3020grid[1]
0x3040grid[2]
0x3060grid[3]
0x3080grid[4]
1 / 12
Spiral

Anti-clockwise Spiral

The same wall-walking algorithm, only the walk starts on the left edge and turns the other way. The border-shrink logic is unchanged — direction is just a different order of the four walks.

The size-5 grid

The same 5-by-5 matrix and the same 1 to 25 counter, but the walk starts down the left column, then across the bottom, up the right side and back along the top. Only the order of the four walks changes, not the numbering.

Off-by-one traps

Reversing the counter instead of the walk produces a mirrored numbering that no longer matches the border-shrink order; the direction belongs in the sequence of edges, not in the value.

Step by step

  1. Set top, bottom, left, right as usual.
  2. Walk down the left column first; then left++.
  3. Walk right along the bottom row; then bottom--.
  4. Walk up the right column; then right--.
  5. Walk left along the top row; then top++.
  6. Repeat while the borders stay ordered.

Complexity

BestO(n²)
AverageO(n²)
WorstO(n²)
SpaceO(n²) — a full matrix is needed

When to use it

  • Rotating the spiral direction
  • Re-using one traversal skeleton
Study tip: pick a size, then run and step through the execution. Watch the grid[r] rows in the Heap region and the i / j / count registers on the Stack — every printed character is one write to the grid.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits