Skip to main content

46. Rotate Image

mediumAsked at Vercel

Rotate an n x n matrix 90 degrees clockwise in-place. Vercel asks this for the transpose-then-reverse trick — and to see if you can reason about coordinate transformations cleanly.

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; in-place expected.
  • Blind (2026-Q1)Listed in Vercel platform 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 to new matrix

Allocate a new n x n; for each (i, j), set new[j][n-1-i] = old[i][j]; copy back.

Time
O(n^2)
Space
O(n^2)
function rotate(matrix) {
  const n = matrix.length;
  const tmp = Array.from({length: n}, () => new Array(n));
  for (let i = 0; i < n; i++) for (let j = 0; j < n; j++) tmp[j][n - 1 - i] = matrix[i][j];
  for (let i = 0; i < n; i++) for (let j = 0; j < n; j++) matrix[i][j] = tmp[i][j];
}

Tradeoff: Violates in-place constraint. Mention only to motivate the trick.

2. Transpose then reverse rows (optimal)

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 (let i = 0; i < n; i++) matrix[i].reverse();
}

Tradeoff: Two clean O(n^2) sweeps. The transpose-then-reverse composition equals a 90 degree clockwise rotation — easier to verify than a single 4-element cycle.

Vercel-specific tips

Vercel grades the transpose-then-reverse trick. Bonus signal: verifying with a small example (3x3) on the whiteboard before coding, and offering the 4-element cycle alternative (rotate 4 corners at once) for the candidate who wants to do it in one pass.

Common mistakes

  • Transposing the full n x n (both halves) — swaps each pair twice, undoing the work. Must iterate j > i only.
  • Forgetting the row reversal — that gives the transpose, not the rotation.
  • Coordinate-juggling without sanity-checking on a 2x2 — easy to flip the wrong axis.

Follow-up questions

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

  • Rotate counterclockwise — transpose then reverse columns.
  • Rotate 180 degrees — reverse both axes.
  • Rotate by arbitrary angle in a continuous coordinate space (non-LC).

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 does transpose + reverse rows = 90-degree clockwise?

Transposing reflects across the main diagonal: (i,j) -> (j,i). Reversing each row reflects across the vertical center: (j,i) -> (j, n-1-i). Composing two reflections is a rotation; in this case, 90 degrees clockwise.

Could I do it in one pass with the 4-element cycle?

Yes — rotate the four corners of each layer simultaneously. Trickier indexing but uses fewer writes. Either is acceptable; transpose-then-reverse is what Vercel typically wants.

Practice these live with InterviewChamp.AI

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