Skip to content
Optimal Lab

Matrix · 133Medium

Spiral Matrix

Given an m x n matrix, return all its elements in spiral order: right across the top, down the right side, left across the bottom, up the left side, then inward.

Constraints

  • m == matrix.length, n == matrix[i].length
  • 1 <= m, n <= 10
  • -100 <= matrix[i][j] <= 100

Examples

spiralOrder([[1,2,3],[4,5,6],[7,8,9]])  ->  [1,2,3,6,9,8,7,4,5]
spiralOrder([[1, 2, 3, 4]])             ->  [1, 2, 3, 4]         // single row
spiralOrder([[1], [2], [3]])            ->  [1, 2, 3]            // single column
spiralOrder([[1]])                      ->  [1]                  // 1x1
spiralOrder([[1,2],[3,4]])              ->  [1,2,4,3]
spiralOrder([[1,2,3],[4,5,6]])          ->  [1,2,3,6,5,4]        // wider than tall

Edge cases

  • A single row or single column must not be traversed twice.
  • Non-square matrices.
Show the target complexity (spoiler)
Naive
Marking visited cells in a parallel grid works but costs O(m * n) extra space.
Target
O(m * n) time, O(1) extra space — four boundary pointers (top, bottom, left, right) contracted after each pass.

Solve it

By hand, in your editor. The runner checks answers and timing.

git clone https://github.com/sarmadkung/optimal-round
cd optimal-round
# open problems/16-matrix/133-spiral-matrix.js
./practice c 133