48. Rotate Image
mediumAsked at SnowflakeRotate an n x n 2D matrix 90 degrees clockwise in place. Snowflake asks this to test in-place matrix transformation — directly relevant to transpose-and-swap operations on column chunks during their executor's data shuffling.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Snowflake loops.
- Glassdoor (2025-Q4)— Snowflake execution-engine team uses this in onsites.
- LeetCode Discuss (2025-10)— Reported at Snowflake new-grad screens.
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. Copy into new matrix
Allocate new matrix; new[j][n-1-i] = old[i][j].
- Time
- O(n^2)
- Space
- O(n^2)
function rotate(matrix) {
const n = matrix.length;
const copy = Array.from({length: n}, () => new Array(n));
for (let i = 0; i < n; i++) for (let j = 0; j < n; j++) copy[j][n - 1 - i] = matrix[i][j];
for (let i = 0; i < n; i++) for (let j = 0; j < n; j++) matrix[i][j] = copy[i][j];
}Tradeoff: Violates the in-place constraint.
2. Transpose then reverse rows (optimal)
Step 1: transpose matrix in place (swap matrix[i][j] and matrix[j][i] for i < j). Step 2: 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 simple passes, O(1) space. Beats the layer-by-layer 4-rotation approach in clarity.
Snowflake-specific tips
Snowflake interviewers want the transpose-then-reverse approach for its clarity. Bonus signal: connect to column-to-row transpose during columnar-to-row reshuffle — when their executor needs to ship a batch row-by-row for an external operator, the in-place transpose pattern lights up.
Common mistakes
- Looping j from 0 in transpose — swaps each pair twice and gets you back to the original.
- Reversing all rows AND all columns — that's 180 degrees, not 90.
- Trying to rotate one cell at a time via 4-tuple cycles — works but harder to get right.
Follow-up questions
An interviewer at Snowflake may pivot to one of these next:
- Rotate counter-clockwise — transpose then reverse columns (or swap the two steps).
- Rotate by 180 degrees.
- Spiral Matrix (LC 54).
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why does transpose + row-reverse equal 90 clockwise?
Transpose maps (i,j) to (j,i). Reversing the row maps (j,i) to (j, n-1-i). Composition: (i,j) -> (j, n-1-i), which is 90 clockwise.
Why j = i+1 in transpose?
j > i avoids swapping each pair twice (which would undo the transpose). The diagonal (i == j) is fixed.
Practice these live with InterviewChamp.AI
Drill Rotate Image and other Snowflake interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →