48. Spiral Matrix
mediumAsked at VercelGiven a matrix, return all elements in spiral order. Vercel asks this to see if you can manage four mutually-shrinking bounds without off-by-one errors — a clean test of disciplined iteration.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Vercel loops.
- Glassdoor (2025-12)— Vercel platform onsite; boundary-tracking expected.
- Blind (2026-Q1)— Listed in Vercel screen pool.
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 + visited matrix
Walk in 4 directions; turn when you hit a wall 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;
const 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: O(mn) extra space for visited. Boundary-shrinking version is cleaner.
2. Four-boundary shrinking (optimal)
Maintain top, bottom, left, right. Walk right (top++), down (right--), left (bottom--), up (left++). Stop when boundaries cross.
- Time
- O(m*n)
- Space
- O(1) extra
function spiralOrder(matrix) {
const out = [];
let top = 0, bot = matrix.length - 1;
let left = 0, right = matrix[0].length - 1;
while (top <= bot && left <= right) {
for (let c = left; c <= right; c++) out.push(matrix[top][c]);
top++;
for (let r = top; r <= bot; r++) out.push(matrix[r][right]);
right--;
if (top <= bot) { for (let c = right; c >= left; c--) out.push(matrix[bot][c]); bot--; }
if (left <= right) { for (let r = bot; r >= top; r--) out.push(matrix[r][left]); left++; }
}
return out;
}Tradeoff: O(1) extra space, four for-loops per layer. The `if` guards on the last two loops handle non-square shapes (e.g., 1xN) that would otherwise repeat rows/columns.
Vercel-specific tips
Vercel grades the boundary version. Bonus signal: the two `if` guards (must check top <= bot before walking left, and left <= right before walking up) — these are the classic off-by-one bugs that fail on 1xN or Nx1 matrices.
Common mistakes
- Forgetting the `if` guards on the bottom/left passes — duplicate elements on single-row/column matrices.
- Updating boundaries before the loop emits — off-by-one.
- Using a visited matrix when boundary-tracking is cleaner.
Follow-up questions
An interviewer at Vercel may pivot to one of these next:
- Spiral Matrix II (LC 59) — fill an empty matrix with 1..n^2 in spiral.
- Spiral Matrix III (LC 885) — start from arbitrary position.
- Diagonal traversal (LC 498) — related coordinate-juggling.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why the two extra `if` guards?
On a 1-row matrix, after walking right, top++ makes top > bot. Without the guard, the leftward walk would re-emit the same row. Same for 1-column shapes.
Could I do it recursively?
Yes — peel the outer layer and recurse on the inner submatrix. Conceptually clean, but boundary-tracking is more efficient (no recursion stack).
Practice these live with InterviewChamp.AI
Drill Spiral Matrix and other Vercel interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →