Skip to main content

48. Rotate Image

mediumAsked at Reddit

Rotate an n x n matrix 90 degrees clockwise in-place. Reddit asks this to test the transpose-then-reverse trick — the same building block they use when re-orienting heatmap matrices for moderator dashboards.

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

Source citations

Public interview reports confirming this problem appears in Reddit loops.

  • Glassdoor (2026-Q1)Reddit phone screen for backend roles.

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

Compute new[j][n-1-i] = matrix[i][j].

Time
O(n^2)
Space
O(n^2)
// Anti-pattern: violates in-place constraint.

Tradeoff: Fails the in-place requirement.

2. Transpose + reverse rows (optimal)

First transpose (swap matrix[i][j] with matrix[j][i]), 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: O(1) extra. Two simple passes are clearer than one tricky 4-cell rotation.

Reddit-specific tips

Reddit interviewers will accept either the transpose+reverse or the 4-cell rotation. Bonus signal: explain WHY transpose+reverse works — matrix transpose plus horizontal flip equals 90° CW rotation. Mention the counter-clockwise variant uses transpose+reverse-cols.

Common mistakes

  • Looping j from 0 to n in the transpose step (double-swaps and undoes itself). Use j = i + 1.
  • Reversing columns instead of rows (gives counter-clockwise).
  • Allocating a new matrix and pretending it's in-place.

Follow-up questions

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

  • Rotate 180 degrees — reverse rows then reverse cols.
  • Spiral matrix (LC 54).
  • Rotate counter-clockwise 90.

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 j = i + 1 in transpose?

Swapping (i, j) and (j, i) is symmetric — doing it twice undoes it. Only swap the upper triangle.

Alternative algorithm?

Rotate four cells at a time — (i, j), (j, n-1-i), (n-1-i, n-1-j), (n-1-j, i). Equivalent but harder to code without bugs.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →