46. Rotate Image
mediumAsked at PlaidRotate an NxN matrix 90 degrees clockwise in place. Plaid asks this because in-place 2D transforms are the same shape as their column-major to row-major balance-history transposition for charts.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Plaid loops.
- Glassdoor (2025)— Plaid SWE II OA — matrix transform.
- LeetCode Discuss (2026)— Plaid in-place classic.
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].length1 <= n <= 20-1000 <= matrix[i][j] <= 1000
Examples
Example 1
matrix = [[1,2,3],[4,5,6],[7,8,9]][[7,4,1],[8,5,2],[9,6,3]]Example 2
matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]][[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]Approaches
1. Allocate new matrix
Build a fresh matrix with rotated indices; copy back.
- Time
- O(n^2)
- Space
- O(n^2)
function rotate(matrix) {
const n = matrix.length;
const r = Array.from({ length: n }, () => new Array(n));
for (let i = 0; i < n; i++) for (let j = 0; j < n; j++) r[j][n - 1 - i] = matrix[i][j];
for (let i = 0; i < n; i++) for (let j = 0; j < n; j++) matrix[i][j] = r[i][j];
}Tradeoff: Violates the in-place constraint. Mention only as the warm-up.
2. Transpose then reverse each row
Swap matrix[i][j] with matrix[j][i] (transpose). Then reverse each row. Composition = 90° clockwise rotation.
- 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: In-place. Two clean phases — easy to debug, easy to test. The j = i+1 (not 0) in transpose prevents double-swap.
Plaid-specific tips
Plaid grades this on the transpose-then-reverse decomposition because it's the readable production approach. The 4-way cycle swap is more clever but harder to debug. Bonus signal: derive the composition out loud — transpose followed by horizontal flip equals 90° clockwise.
Common mistakes
- Starting j from 0 in the transpose — double-swaps and ends up with the original matrix.
- Reversing columns instead of rows — produces a 90° counter-clockwise rotation.
- Allocating a new matrix — fails the in-place constraint.
Follow-up questions
An interviewer at Plaid may pivot to one of these next:
- Rotate counter-clockwise (transpose then reverse columns).
- Rotate by 180° (reverse rows then reverse columns).
- Rotate a non-square matrix.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why does transpose + reverse work?
Transpose maps (i,j) to (j,i). Row reverse maps (j,i) to (j, n-1-i). Composition: (i,j) -> (j, n-1-i), which is the 90° clockwise formula.
Could you do this with a single cycle-swap pass?
Yes — four cells form a cycle: (i,j) -> (j, n-1-i) -> (n-1-i, n-1-j) -> (n-1-j, i) -> back. One temp variable per cycle. Equivalent asymptotic, fewer writes.
Practice these live with InterviewChamp.AI
Drill Rotate Image and other Plaid interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →