46. Rotate Image
mediumAsked at SalesforceRotate an n x n 2D matrix 90 degrees clockwise in place. Salesforce uses this to test the transpose-then-reverse trick that avoids the layer-by-layer approach.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Salesforce loops.
- Glassdoor (2026-Q1)— Salesforce uses matrix rotations as a brain-teaser warmup.
- LeetCode Discuss (2025-09)— Tests the transpose+reverse insight.
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. Use auxiliary matrix
Create a new matrix; assign new[j][n-1-i] = old[i][j].
- Time
- O(n^2)
- Space
- O(n^2)
function rotate(matrix) {
const n = matrix.length;
const rotated = Array.from({ length: n }, () => new Array(n));
for (let i = 0; i < n; i++) for (let j = 0; j < n; j++) rotated[j][n - 1 - i] = matrix[i][j];
for (let i = 0; i < n; i++) for (let j = 0; j < n; j++) matrix[i][j] = rotated[i][j];
}Tradeoff: Violates the in-place constraint.
2. Transpose then reverse rows
Transpose in place (swap matrix[i][j] with matrix[j][i] for i < j). Then reverse each row.
- Time
- O(n^2)
- Space
- O(1)
function rotate(matrix) {
const n = matrix.length;
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]];
}
}
for (let i = 0; i < n; i++) matrix[i].reverse();
}Tradeoff: Two O(n^2) passes but O(1) extra space. The transpose + reverse-rows trick is the textbook in-place 90-degree rotation.
Salesforce-specific tips
Salesforce specifically tests transpose-then-reverse because it generalizes cleanly: 180 degrees is reverse rows + reverse cols; 270 degrees is transpose + reverse cols. Bonus signal: mention all four rotations have O(1) variants and demonstrate the pattern.
Common mistakes
- Looping i from 0 to n and j from 0 to n during transpose — double-swaps, cancels out.
- Reversing columns instead of rows — produces counter-clockwise rotation.
- Trying layer-by-layer rotation — works but more complex and error-prone.
Follow-up questions
An interviewer at Salesforce may pivot to one of these next:
- Rotate 90 counter-clockwise — transpose + reverse cols.
- Rotate by 180 — reverse rows AND reverse cols.
- Generalize to a non-square matrix.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why j starts at i+1 in the transpose loop?
Because swapping (i,j) with (j,i) twice cancels out. Looping j from i+1 ensures each pair is swapped exactly once.
Can I just reverse the whole matrix as one array?
No — matrix is row-major, but a 90-degree rotation interleaves rows and columns. Flat reverse is a 180-degree rotation.
Practice these live with InterviewChamp.AI
Drill Rotate Image and other Salesforce interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →