54. Spiral Matrix
mediumAsked at NetflixGiven an m x n matrix, return all elements in spiral order. Netflix asks this to test whether you can manage four-boundary state without going out of bounds — the canonical 'index gymnastics' question.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Netflix loops.
- Glassdoor (2026-Q1)— Netflix L4 onsite reports cite this as the iterative-matrix problem on the algorithms screen.
- Blind (2025-10)— Netflix SWE writeups describe this as the bounds-management warmup before a harder follow-up.
Problem
Given an m x n matrix, return all elements of the matrix in spiral order (clockwise, starting from the top-left).
Constraints
m == matrix.lengthn == matrix[i].length1 <= m, n <= 10-100 <= matrix[i][j] <= 100
Examples
Example 1
matrix = [[1,2,3],[4,5,6],[7,8,9]][1,2,3,6,9,8,7,4,5]Example 2
matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]][1,2,3,4,8,12,11,10,9,5,6,7]Approaches
1. Direction array + visited matrix
Use an array of (dr, dc) for the four directions. Walk step by step; if next cell is out of bounds or visited, rotate direction.
- Time
- O(m * n)
- Space
- O(m * n) for visited
function spiralOrderVisited(matrix) {
const m = matrix.length, n = matrix[0].length;
const out = [];
const seen = Array.from({ length: m }, () => new Array(n).fill(false));
const dirs = [[0, 1], [1, 0], [0, -1], [-1, 0]];
let r = 0, c = 0, d = 0;
for (let i = 0; i < m * n; i++) {
out.push(matrix[r][c]);
seen[r][c] = true;
const nr = r + dirs[d][0];
const nc = c + dirs[d][1];
if (nr < 0 || nr >= m || nc < 0 || nc >= n || seen[nr][nc]) d = (d + 1) % 4;
r += dirs[d][0]; c += dirs[d][1];
}
return out;
}Tradeoff: Easiest to reason about. The visited matrix uses O(m * n) extra space which is acceptable here but rejected by senior interviewers who want O(1) extra.
2. Four boundaries (optimal)
Maintain top, bottom, left, right boundaries. Walk top row left-to-right, then right column top-to-bottom, then bottom row right-to-left (if rows remain), then left column bottom-to-top (if cols remain). Shrink each boundary after use.
- Time
- O(m * n)
- Space
- O(1) extra (excluding output)
function spiralOrder(matrix) {
const out = [];
let top = 0, bottom = matrix.length - 1;
let left = 0, right = matrix[0].length - 1;
while (top <= bottom && left <= right) {
for (let c = left; c <= right; c++) out.push(matrix[top][c]);
top++;
for (let r = top; r <= bottom; r++) out.push(matrix[r][right]);
right--;
if (top <= bottom) {
for (let c = right; c >= left; c--) out.push(matrix[bottom][c]);
bottom--;
}
if (left <= right) {
for (let r = bottom; r >= top; r--) out.push(matrix[r][left]);
left++;
}
}
return out;
}Tradeoff: O(1) extra space. The two `if` checks after the second pair of for-loops are critical — they prevent re-visiting a row/column when the spiral has narrowed to a single line.
Netflix-specific tips
Netflix interviewers grade two specific things: (1) Do you understand WHY the two `if` checks (top <= bottom and left <= right) are needed in the boundaries version? They prevent re-traversing the last row or column. (2) Can you trace through a 1xN or Nx1 matrix in your head? Most candidates' first version fails one of those edge cases.
Common mistakes
- Forgetting the inner `if (top <= bottom)` check before the right-to-left pass — overwrites the same row in a 1xN matrix.
- Using a single visited matrix when the boundaries approach is cleaner — works but signals you didn't see the better solution.
- Off-by-one on the inner for-loop bounds (`c < right` vs `c <= right`).
Follow-up questions
An interviewer at Netflix may pivot to one of these next:
- Spiral Matrix II (LC 59) — generate an n x n matrix filled in spiral order.
- Spiral Matrix III (LC 885) — start at (r, c) and walk a spiral covering an R x C grid.
- Rotate Image (LC 48) — related index-gymnastics warmup.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why is the four-boundaries version preferred?
It uses O(1) extra space and the loop structure makes the spiral pattern explicit — anyone reading the code can see the four sides. The visited-matrix version is correct but less expressive.
What happens with a single row or column?
The two inner `if` checks handle this. For a 1xN matrix, after the top-row pass `top` becomes 1 and `top > bottom` halts the inner branches — preventing double-counting.
Free learning resources
Curated free links for this problem.
Practice these live with InterviewChamp.AI
Drill Spiral Matrix and other Netflix interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →