Rotate Image is the #19 most asked LeetCode problem globally — the classic matrix transformation problem that tests in-place manipulation and geometric intuition.
Our analysis of 10,385 verified interview questions across 259 companies shows it appeared 60 times across 20 companies — including Google, Amazon, Meta, Microsoft, Goldman Sachs, Bloomberg, DE Shaw, J.P. Morgan, Capital One, and Mastercard.
The key insight is a two-step decomposition: transpose the matrix (swap matrix[i][j] with matrix[j][i]), then reverse each row. These two operations combine to produce a 90° clockwise rotation — and both can be done in-place in O(n²) time and O(1) space.
| Metric | Value |
|---|---|
| Total Interview Appearances | 60 verified mentions |
| Companies That Ask It | 20 companies |
| Global Rank | #19 out of 10,385 questions |
| Difficulty | Medium |
| Core Patterns | Array, Math, Matrix |
Google, Amazon, Meta, Microsoft, Goldman Sachs, Bloomberg, DE Shaw, J.P. Morgan, Capital One, Mastercard, AMD, Accenture, Adobe, Apple, Cisco, ConsultAdd, Flipkart, IBM, Infosys, and Intel.
LeetCode #48 — Rotate Image
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees clockwise. You must rotate the image in-place — do not allocate another 2D matrix.
Input: [[1,2,3],[4,5,6],[7,8,9]]
Output: [[7,4,1],[8,5,2],[9,6,3]]
Input: [[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]]
A 90° clockwise rotation maps element at (i, j) to position (j, n-1-i). This can be decomposed into two simpler operations:
matrix[i][j] with matrix[j][i] for all i < j. After this, element at (i,j) moves to (j,i).(j,i) moves to (j, n-1-i).Original: After Transpose: After Row Reverse:
1 2 3 1 4 7 7 4 1
4 5 6 →→→ 2 5 8 →→→ 8 5 2
7 8 9 3 6 9 9 6 3 ✅ 90° CW
def rotate(matrix: list[list[int]]) -> None:
n = len(matrix)
# Step 1: Transpose (swap across main diagonal)
for i in range(n):
for j in range(i + 1, n):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
# Step 2: Reverse each row
for row in matrix:
row.reverse()
class Solution {
public void rotate(int[][] matrix) {
int n = matrix.length;
// Step 1: Transpose
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
// Step 2: Reverse each row
for (int[] row : matrix) {
int left = 0, right = n - 1;
while (left < right) {
int temp = row[left];
row[left++] = row[right];
row[right--] = temp;
}
}
}
}
var rotate = function(matrix) {
const n = matrix.length;
// Step 1: 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]];
// Step 2: Reverse each row
for (const row of matrix)
row.reverse();
};
| Rotation | Steps |
|---|---|
| 90° Clockwise | Transpose → Reverse each row |
| 90° Counter-Clockwise | Reverse each row → Transpose |
| 180° | Reverse each row → Reverse each column |
Tag on DSAPrep.dev under Matrix and Math.