O16
Spiral Matrix Traversal
Problem Statement & Objective
Given an m x n matrix, return all elements of the matrix in spiral order (clockwise starting from top-left).
Function Signature (Python)
spiral_order(matrix: list[list[int]]) -> list[int]Input & Output Contract
Output Rule:List of numbers ordered along the clockwise spiral path.
System Constraints
m == matris.length
n == matris[i].length
1 <= m, n <= 100
-100 <= matris[i][j] <= 100Example Execution Walkthrough
matris = [[1,2,3],[4,5,6],[7,8,9]]
result: [1,2,3,6,9,8,7,4,5]Example Test Scenarios (7)
Example 1: 3x3
Input:
[[[1,2,3],[4,5,6],[7,8,9]]]Expected Output:
[1,2,3,6,9,8,7,4,5]Example 2: 3x4
Input:
[[[1,2,3,4],[5,6,7,8],[9,10,11,12]]]Expected Output:
[1,2,3,4,8,12,11,10,9,5,6,7]Example 3: 1x1
Input:
[[[7]]]Expected Output:
[7]Empty Matrix
Input:
[[]]Expected Output:
[]Empty Row
Input:
[[[]]]Expected Output:
[]Single Row
Input:
[[[1,2,3]]]Expected Output:
[1,2,3]Single Column
Input:
[[[1],[2],[3]]]Expected Output:
[1,2,3]