Spiral Matrix - Problem

Imagine you're a treasure hunter exploring a rectangular grid filled with precious gems. Your goal is to collect all the gems by walking in a spiral pattern - starting from the top-left corner, moving right along the top edge, then down the right edge, left along the bottom, up the left edge, and continuing this spiral pattern inward until you've collected every single gem!

Given an m x n matrix, return all elements traversed in clockwise spiral order. You'll start at matrix[0][0] and spiral inward, collecting elements as you go.

Example: For matrix [[1,2,3],[4,5,6],[7,8,9]], the spiral path would be: 1 → 2 → 3 → 6 → 9 → 8 → 7 → 4 → 5, forming a clockwise spiral from outside to inside.

Input & Output

example_1.py — Standard 3x3 Matrix
$ Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output: [1,2,3,6,9,8,7,4,5]
💡 Note: Start at top-left (1), go right (2,3), down (6,9), left (8,7), up (4), then center (5). This creates a clockwise spiral from outer edge to inner center.
example_2.py — Rectangular Matrix
$ Input: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
Output: [1,2,3,4,8,12,11,10,9,5,6,7]
💡 Note: For non-square matrices, continue the spiral pattern. Go right across top row (1,2,3,4), down right edge (8,12), left across bottom (11,10,9), up left edge (5), then complete inner elements (6,7).
example_3.py — Single Row
$ Input: matrix = [[1,2,3,4]]
Output: [1,2,3,4]
💡 Note: Edge case: single row matrix. Simply traverse left to right since there's no spiraling possible - just one horizontal line of elements.

Visualization

Tap to expand
Spiral Matrix Visualization123456789ENDTraversal Order:1. Right: 1 → 2 → 32. Down: 6 → 93. Left: 8 → 74. Up: 45. Center: 5Result: [1,2,3,6,9,8,7,4,5]Key Insight:Maintain 4 boundaries and shrinkthem after completing each edge
Understanding the Visualization
1
Set Initial Boundaries
Define top=0, bottom=m-1, left=0, right=n-1 as our outer layer
2
Traverse Right Edge
Move right across the top row, collecting elements from left to right
3
Shrink Top Boundary
Increment top boundary since we've completed the top row
4
Traverse Down Edge
Move down the right column from top to bottom
5
Continue Spiral Pattern
Repeat for left traversal, up traversal, shrinking boundaries each time
6
Complete All Layers
Continue until all boundaries have been processed
Key Takeaway
🎯 Key Insight: By maintaining four boundaries and systematically shrinking them after each edge traversal, we can simulate the spiral pattern efficiently without extra space for tracking visited cells.

Time & Space Complexity

Time Complexity
⏱️
O(m*n)

Visit each cell exactly once in the m×n matrix

n
2n
Linear Growth
Space Complexity
O(1)

Only using constant extra space for boundary variables (excluding result array)

n
2n
Linear Space

Constraints

  • m == matrix.length
  • n == matrix[i].length
  • 1 ≤ m, n ≤ 10
  • -100 ≤ matrix[i][j] ≤ 100
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
52.3K Views
High Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen