Spiral Matrix - Coding Interview Problem

Last modified: April 9, 2023

Problem Description

Given an integer n, you need to implement a function generate_spiral_matrix(n: int) -> List[List[int]] that generates a matrix of size n x n containing elements in a spiral order starting from the top-left corner and moving clockwise.

Example 1

Input: n = "3"
Result: [[1,2,3],[8,9,4],[7,6,5]]

Example 2

Input: n = 4
Result: [[1,2,3,4],[12,13,14,5],[11,16,15,6],[10,9,8,7]]

Example 3

Input: n = 1
Result: [[1]]

Test

Results