Skip to content

Spiral Matrix

MediumPython 3MatricesArrays

You are given an integer n. Return an n x n matrix filled with the integers 1, 2, ..., n*n laid out in clockwise spiral order, starting in the top-left corner and moving right first.

Constraints:
  - 1 <= n <= 100
Examples:
  n = 1 -> [[1]]
  n = 2 -> [[1, 2],
            [4, 3]]
  n = 3 -> [[1, 2, 3],
            [8, 9, 4],
            [7, 6, 5]]
  n = 4 -> [[ 1,  2,  3, 4],
            [12, 13, 14, 5],
            [11, 16, 15, 6],
            [10,  9,  8, 7]]

Python 3, run in your browser. Run tests runs the problem's own tests with a free account; the AI interviewer reads your code and your runs in a practice interview.

Loading the editor…