Skip to main content

16. Rotate Image

mediumAsked at Adobe

Rotate an n×n matrix 90 degrees clockwise in-place. Adobe is a graphics and imaging company — in-place 2D array transformations are a core competency that appears directly in image rotation, canvas transforms, and pixel buffer manipulation across Photoshop, Lightroom, and Illustrator.

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

Source citations

Public interview reports confirming this problem appears in Adobe loops.

  • Glassdoor (2026-02)Adobe SDE-II onsite reports cite matrix rotation as one of the most common 2D array problems.
  • LeetCode Discuss (2025-12)Multiple Adobe candidates confirm rotate image appears in coding rounds due to its graphics relevance.

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, meaning you have to modify the input 2D matrix directly. Do not allocate another 2D matrix.

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]]

Explanation: Transpose then reverse each row.

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. Extra matrix copy

Create a new n×n matrix and map matrix[i][j] to result[j][n-1-i], then copy back.

Time
O(n^2)
Space
O(n^2)
function rotate(matrix) {
  const n = matrix.length;
  const copy = matrix.map(row => [...row]);
  for (let i = 0; i < n; i++) {
    for (let j = 0; j < n; j++) {
      matrix[j][n - 1 - i] = copy[i][j];
    }
  }
}

Tradeoff: Correct but violates the in-place constraint. Adobe will ask you to eliminate the extra O(n^2) space.

2. Transpose then reverse rows

A 90-degree clockwise rotation = transpose (swap matrix[i][j] with matrix[j][i]) followed by reversing each row. This is a well-known two-pass trick that achieves true in-place rotation with O(1) extra space beyond the input matrix itself.

Time
O(n^2)
Space
O(1)
function rotate(matrix) {
  const n = matrix.length;
  // Step 1: 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]];
    }
  }
  // Step 2: reverse each row
  for (let i = 0; i < n; i++) {
    matrix[i].reverse();
  }
}

Tradeoff: O(n^2) time, O(1) space. The transpose-then-reverse insight is the expected answer at Adobe. Derive it by tracing where (0,0) goes in a 90-degree rotation: (0,0) -> (0, n-1). After transpose (0,0) becomes (0,0) staying at first column; reversing the row sends column 0 to column n-1. The math works out.

Adobe-specific tips

Adobe interviewers specifically look for the transpose-then-reverse decomposition because it demonstrates understanding of geometric transformations as compositions of simpler operations — the same principle underlying transformation matrices in Illustrator and Photoshop. Be ready to generalize: counter-clockwise rotation = reverse each row then transpose; 180-degree rotation = reverse rows and reverse columns.

Common mistakes

  • Transposing the full matrix (both triangles) which double-swaps and leaves the matrix unchanged.
  • Reversing columns instead of rows after the transpose, producing counter-clockwise rotation.
  • Attempting an in-place 4-cell cycle approach without correctly handling the n/4 groups.

Follow-up questions

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

  • How would you rotate the matrix counter-clockwise 90 degrees?
  • How would you rotate by 180 degrees?
  • How does this extend to non-square (m×n) matrices — why can't you do it in-place?

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 then reverse rows give a 90-degree clockwise rotation?

After transpose, element at (i,j) is now at (j,i). Reversing row j maps position j in the row to position n-1-j. Composing these: original (i,j) ends up at (j, n-1-i), which is exactly the 90-degree clockwise mapping.

Can you do this in a single pass (4-cell cycle)?

Yes — for each cell (i,j) in the top-left quadrant, rotate four cells in a cycle: temp = top, top = left, left = bottom, bottom = right, right = temp. This is O(n^2) time O(1) space with one pass but requires careful index math.

Practice these live with InterviewChamp.AI

Drill Rotate Image and other Adobe interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →