48. Rotate Image
mediumAsked at CiscoRotate Image at Cisco tests whether you can manipulate a 2D matrix in place without falling back to an auxiliary matrix. The interviewer wants the transpose-then-reverse trick because it generalizes to any 90-degree rotation and uses zero extra space.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Cisco loops.
- Glassdoor (2026-Q1)— Cisco SDE-II interview reports cite Rotate Image as a 20-30 minute matrix round.
- Blind (2025-09)— Cisco interview thread lists Rotate Image as a recurring problem.
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. Brute-force: allocate a fresh matrix
Read every cell from the input and write into result[j][n-1-i].
- Time
- O(n^2)
- Space
- O(n^2)
function rotateBrute(matrix) {
const n = matrix.length;
const result = Array.from({ length: n }, () => new Array(n));
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
result[j][n - 1 - i] = matrix[i][j];
}
}
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
matrix[i][j] = result[i][j];
}
}
}Tradeoff: Violates the in-place constraint — Cisco interviewers reject this immediately. Useful only to anchor the index math before optimizing.
2. Transpose, then reverse each row (optimal)
Two passes: (1) transpose the matrix (swap matrix[i][j] with matrix[j][i] for i < j), (2) reverse each row in place. Composition of those two operations is a 90-degree 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 (let i = 0; i < n; i++) {
matrix[i].reverse();
}
}Tradeoff: Zero extra space, idiomatic. The key insight: a 90-degree rotation = reflect across main diagonal + reflect across vertical center. The 'j = i + 1' bound on the transpose loop is what prevents double-swapping (which would undo the transpose).
3. Four-way cyclic swap (also optimal)
Rotate four cells at a time — for each (i, j) in the top-left quadrant, swap matrix[i][j], matrix[j][n-1-i], matrix[n-1-i][n-1-j], matrix[n-1-j][i].
- Time
- O(n^2)
- Space
- O(1)
function rotateCyclic(matrix) {
const n = matrix.length;
for (let i = 0; i < Math.floor(n / 2); i++) {
for (let j = i; j < n - 1 - i; j++) {
const tmp = matrix[i][j];
matrix[i][j] = matrix[n - 1 - j][i];
matrix[n - 1 - j][i] = matrix[n - 1 - i][n - 1 - j];
matrix[n - 1 - i][n - 1 - j] = matrix[j][n - 1 - i];
matrix[j][n - 1 - i] = tmp;
}
}
}Tradeoff: Single pass, identical Big-O. The index math is harder to get right under interview pressure. Cisco interviewers respect the cleaner transpose-then-reverse version unless you specifically claim 'single pass.'
Cisco-specific tips
Cisco interviewers grade this on the index math AND the verbal explanation. Say the geometric framing FIRST: '90-degree rotation = transpose + horizontal flip.' Then write the two loops. The `j = i + 1` bound is the gotcha — write it and explain 'I skip the diagonal because swapping it with itself is a no-op AND skip the lower triangle because those swaps were already done.'
Common mistakes
- Writing the transpose loop as `for j = 0 to n` — that double-swaps every cell and ends with the original matrix.
- Forgetting to reverse the rows — half the algorithm; produces a transposed matrix, not a rotated one.
- Mixing up clockwise and counterclockwise — clockwise = transpose + reverse-rows; counterclockwise = transpose + reverse-cols (or reverse-rows then transpose).
Follow-up questions
An interviewer at Cisco may pivot to one of these next:
- Spiral Matrix (LC 54) — traverse in spiral order.
- Set Matrix Zeroes (LC 73) — in-place using first row/col as flags.
- Rotate by 180 or 270 degrees — what changes?
- Rotate a rectangular (m x n) matrix by 90 — requires allocation, not in-place possible.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why does transpose + reverse equal 90-degree clockwise?
Transpose maps (i, j) -> (j, i) — it's a reflection across the main diagonal. Reversing each row then maps (j, i) -> (j, n-1-i). Composition is (i, j) -> (j, n-1-i), which is exactly the 90-degree clockwise rule. Geometrically: 'reflect across diagonal, then mirror horizontally.'
Should I default to transpose+reverse or cyclic swap?
Transpose+reverse for the whiteboard — it's two simple loops with obvious correctness. Use cyclic swap only if the interviewer asks for 'single pass.' Cisco interviewers care more about clarity than performance on n=20.
Free learning resources
Curated free links for this problem.
Practice these live with InterviewChamp.AI
Drill Rotate Image and other Cisco interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →