Ctrl K

Spiral-Numbered Square

Medium
Build a size × size table and write the numbers from 1 to size² into it in a spiral: start at the top-left corner, move right, turn clockwise whenever you hit an edge or an already filled cell, and wind towards the centre. Return the resulting table as a list of rows.
Example 1
size=2 → [[1, 2], [4, 3]]

1 and 2 go along the top row, then 3 drops into the bottom-right corner and 4 lands to its left.

Example 2
size=4 → [[1, 2, 3, 4], [12, 13, 14, 5], [11, 16, 15, 6], [10, 9, 8, 7]]

The outer ring gets 1–12 and the inner 2 × 2 square gets 13–16.

Example 3
size=1 → [[1]]
Constraints
  • 1 ≤ size ≤ 20
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSpiral Readout MediumNext Step of a Cell Colony MediumZigzag Diagonal Sweep MediumMatch-Three Cascade MediumOutward Spiral Walk Medium
loading Python…
Input
size=2
Expected
[[1, 2], [4, 3]]

Run checks the first 3 tests in your browser. Submit checks all 23 tests on the server, hidden ones included.