Skip to main content

46. Rotate Image

mediumAsked at Workday

Rotate an n x n 2D matrix by 90 degrees clockwise in place. Workday uses this for matrix-manipulation discipline — same shape as transposing a payroll-period attendance grid.

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

You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise). You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.

Constraints

  • n == matrix.length == matrix[i].length
  • 1 <= n <= 20
  • -1000 <= matrix[i][j] <= 1000

Examples

Example 1

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

Example 2

Input
matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
Output
[[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]

Approaches

1. Copy into new matrix

new[j][n-1-i] = old[i][j].

Time
O(n^2)
Space
O(n^2)
// disallowed by prompt — must be in-place

Tradeoff: Easy but violates the in-place requirement.

2. Transpose + reverse rows

Transpose (swap matrix[i][j] with matrix[j][i] for i<j), then reverse each row.

Time
O(n^2)
Space
O(1)
function rotate(matrix) {
  const n = matrix.length;
  // transpose
  for (let i = 0; i < n; i++) {
    for (let j = i + 1; j < n; j++) {
      [matrix[i][j], matrix[j][i]] = [matrix[j][i], matrix[i][j]];
    }
  }
  // reverse each row
  for (const row of matrix) row.reverse();
}

Tradeoff: Two passes but in-place. The transpose+reverse decomposition is the canonical trick.

Workday-specific tips

Workday grades on the transpose+reverse insight. Walk through why it equals a 90-degree clockwise rotation. The transpose inner loop must start at i+1 (not 0) — otherwise you swap twice and undo the work.

Common mistakes

  • Transpose inner loop starting at 0 — double-swaps.
  • Reversing columns instead of rows — gives a different rotation direction.
  • Allocating a new matrix — violates in-place.

Follow-up questions

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

  • Rotate by 180 (LC 48 variant) — reverse rows then reverse columns.
  • Spiral Matrix (LC 54).
  • Set Matrix Zeroes (LC 73).

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 transpose + reverse rows?

Transpose flips along the main diagonal: matrix[i][j] -> matrix[j][i]. Reversing rows then flips columns: matrix[j][i] -> matrix[j][n-1-i]. Composed: matrix[i][j] -> matrix[j][n-1-i], which is the 90-cw rotation.

Four-way swap?

Alternative: in-place 4-cell rotation per quadrant. Same complexity, harder to get right under time pressure.

Practice these live with InterviewChamp.AI

Drill Rotate Image 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 →