You're given an m x n matrix of integers. Walk it in a spiral — start at the top-left, go right across the top row, then down the right side, then left along the bottom, then up the left side — peeling the matrix like an onion, layer by layer, until every cell is visited.
Return all elements of the matrix in the order you visit them, as a single flat list.
if top <= bottom and if left <= right guards stop you from reading those same cells a second time.m and n are independent (each 1 to 10), so rectangular and even single-row or single-column matrices are valid inputs — test those edge shapes.matrix[r][c], you write an incrementing counter into each cell as you sweep the four edges (the "generate a spiral matrix" variant).k lands on and its offset along that ring in O(1)-ish math per ring, avoiding a full traversal — a nice arithmetic-vs-simulation trade-off to discuss.Input: matrix = [[1,2,3],[4,5,6],[7,8,9]] Output: [1,2,3,6,9,8,7,4,5] Right across the top (1,2,3), down the right (6,9), left along the bottom (8,7), up the left (4), then the lone center (5).
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] Outer ring first (1,2,3,4,8,12,11,10,9,5), then the inner row (6,7).
- m == matrix.length - n == matrix[i].length - 1 <= m, n <= 10 - -100 <= matrix[i][j] <= 100
Spiral Matrix is a simulation problem: the statement hands you the exact path, and your job is to trace it without tripping over the edges. The elegant version tracks four shrinking boundaries and peels the matrix ring by ring — the same "boundary walk" that rotates an image in place.
matrix[row][col] and moving along a row or down a column.top, bottom, left, right — that define the unvisited rectangle and shrink as you go.Given an m x n matrix, return all elements in spiral order: right across the top, down the right side, left along the bottom, up the left side, then inward.
Worked example — matrix = [[1,2,3],[4,5,6],[7,8,9]]
1 → 2 → 3 top row
↓
4 5 6 right col: 6
↑ ↓
7 ← 8 ← 9 bottom row: 9 8 7, left col: 4, center: 5
order: 1 2 3 6 9 8 7 4 5 ✓
“Can the matrix be rectangular, not just square?”
Yes — m and n are independent. Single-row and single-column matrices are valid and are the shapes most likely to break naive code.
“Is the matrix guaranteed non-empty?”
Yes (m, n >= 1), so there's always at least one cell.
“What happens on a single row or single column?”
After the top-row and right-column sweeps, the remaining shape can be empty — guards prevent re-reading those cells.
“Return a single flat list in visit order?”
Yes — one list, in the order you touch the cells.
“How large is the matrix?”
Up to 10×10 here. Both approaches are O(m·n) time — the only difference is the boundary version's O(1) extra space.
A couple of questions.
Can the matrix be rectangular, or even a single row or column?
I return one flat list in spiral visit order?
I'll track four boundaries and peel one edge at a time — O(1) extra space.
There's no cleverness to discover in what to do; the statement spells out right, down, left, up. The entire difficulty is not stepping on your own tracks or off the grid — which is a bookkeeping problem, not an algorithmic one.
Instead of an O(m·n) visited matrix and "turn when blocked", track top, bottom, left, right. Read the top edge and increment top; read the right edge and decrement right; and so on. The rectangle shrinks each pass until the fences cross — O(1) extra space.
After the top row and right column, the remaining ring might be a single row or column already consumed. Wrapping the bottom-row sweep in if top <= bottom and the left-column sweep in if left <= right stops you from reading those cells a second time.
| Visited grid | Four boundaries | |
|---|---|---|
| Idea | Turn when you'd hit a wall or a used cell | Peel one edge, shrink its fence |
| Time | O(m·n) | O(m·n) |
| Space | O(m·n) | O(1) |
The full code for both is in the Approaches selector below.
Key takeaway
Spiral Matrix is pure simulation — trace right/down/left/up while shrinking four boundaries (top, bottom, left, right) inward. Read an edge, move its fence, and guard the bottom/left sweeps with top <= bottom / left <= right so thin final rings aren't read twice. O(m·n) time, O(1) space.