Skip to main content

48. Spiral Matrix

mediumAsked at Workday

Return all elements of a matrix in spiral order. Workday uses this to test boundary-shrinking discipline — same shape as walking the perimeter of a fiscal calendar grid before drilling inward.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Workday loops.

  • Glassdoor (2025)Workday SDE2 phone screen.

Problem

Given an m x n matrix, return all elements of the matrix in spiral order.

Constraints

  • m == matrix.length
  • n == matrix[i].length
  • 1 <= m, n <= 10
  • -100 <= matrix[i][j] <= 100

Examples

Example 1

Input
matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output
[1,2,3,6,9,8,7,4,5]

Example 2

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]

Approaches

1. Direction vector with visited

Track current direction and visited set; turn right when blocked.

Time
O(m*n)
Space
O(m*n)
// 4 direction vectors + visited matrix

Tradeoff: Works but uses O(m*n) extra space.

2. Four-pointer boundary shrinking

top, bottom, left, right. Traverse the four sides; shrink each boundary after.

Time
O(m*n)
Space
O(1) extra)
function spiralOrder(matrix) {
  const result = [];
  let top = 0, bottom = matrix.length - 1;
  let left = 0, right = matrix[0].length - 1;
  while (top <= bottom && left <= right) {
    for (let j = left; j <= right; j++) result.push(matrix[top][j]);
    top++;
    for (let i = top; i <= bottom; i++) result.push(matrix[i][right]);
    right--;
    if (top <= bottom) {
      for (let j = right; j >= left; j--) result.push(matrix[bottom][j]);
      bottom--;
    }
    if (left <= right) {
      for (let i = bottom; i >= top; i--) result.push(matrix[i][left]);
      left++;
    }
  }
  return result;
}

Tradeoff: O(1) extra space. The two 'if' guards before bottom and left rows protect against the last-row-or-column case where you'd otherwise double-emit.

Workday-specific tips

Workday wants the four-pointer solution. The two extra 'if' guards (top <= bottom and left <= right before the bottom/left traversals) are the trick — without them, single-row or single-column matrices emit duplicate values.

Common mistakes

  • Missing the guards — single-row matrices emit reversed second pass.
  • Off-by-one with <= vs < on the boundaries.
  • Forgetting to shrink the boundary after each side traversal.

Follow-up questions

An interviewer at Workday may pivot to one of these next:

  • Spiral Matrix II (LC 59) — fill with 1..n^2 in spiral.
  • Spiral Matrix III (LC 885).
  • What if the spiral starts at an arbitrary cell?

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why two extra guards?

On a single-row matrix, after the top row is emitted, top > bottom — we'd re-emit the same row in reverse from the bottom traversal. The guard prevents it.

Counter-clockwise variant?

Swap the order of the four traversals: top, left, bottom, right.

Practice these live with InterviewChamp.AI

Drill Spiral Matrix and other Workday interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →