50. Spiral Matrix
mediumAsked at RedditTraverse a matrix in spiral order. Reddit uses this to test boundary tracking — the same skill needed when iterating around 2D region tiles in their image-grid rendering for AR/AR filters.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Reddit loops.
- Glassdoor (2026-Q1)— Reddit phone screen for backend roles.
Problem
Given an m x n matrix, return all elements of the matrix in spiral order.
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
Track (dr, dc) direction. Turn right when you hit a boundary or a visited cell.
- Time
- O(m*n)
- Space
- O(m*n) for visited
function spiralOrder(matrix) {
const m = matrix.length, n = matrix[0].length;
const visited = 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, out = [];
for (let i = 0; i < m * n; i++) {
out.push(matrix[r][c]); visited[r][c] = true;
const nr = r + dirs[d][0], nc = c + dirs[d][1];
if (nr < 0 || nr >= m || nc < 0 || nc >= n || visited[nr][nc]) d = (d + 1) % 4;
r += dirs[d][0]; c += dirs[d][1];
}
return out;
}Tradeoff: Clean, but requires O(m*n) visited array.
2. Four boundaries shrinking (optimal)
Track top, bottom, left, right. Walk right (top), then down (right), then left (bottom), then up (left). Shrink boundaries after each side.
- Time
- O(m*n)
- Space
- O(1)
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. The two extra guards (if top <= bottom, if left <= right) handle rectangle-not-square cases.
Reddit-specific tips
Reddit interviewers want the four-boundary approach. Bonus signal: explicitly walk through a 3x4 to show why the bottom-row and left-column iterations need guard conditions.
Common mistakes
- Forgetting the guards — duplicates rows/cols when matrix becomes a single row or column.
- Off-by-one (use <= and -- after the loop, or < and don't change).
- Using direction array without revisiting prevention.
Follow-up questions
An interviewer at Reddit may pivot to one of these next:
- Spiral Matrix II (LC 59) — fill a matrix in spiral.
- Spiral Matrix III (LC 885) — start from arbitrary point.
- Output diagonals instead of spiral.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why need top <= bottom check before the third side?
After processing the top row in a single-row matrix, top > bottom; without the guard you'd re-output the only row in reverse.
Could we do this recursively?
Yes — process outer ring, recurse on (m-2 x n-2) inner. Same complexity, more memory.
Practice these live with InterviewChamp.AI
Drill Spiral Matrix and other Reddit interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →