Rotate Image — Solution in Python, Java & C++
Rotate Image solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. A medium problem on arrays and math.
Rotate Image is a medium coding problem on arrays, math and matrix, asked in Capital One and Nutanix interviews.
- Difficulty: Medium
- Topics: Arrays, Math, Matrix
- Asked at: Capital One, Nutanix
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees clockwise and return the result.
The classic challenge solves it in place: transpose, then reverse each row.
Example 1
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output: [[7,4,1],[8,5,2],[9,6,3]]
Example 2
Input: matrix = [[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]]
Constraints
1 <= n <= 8-100 <= matrix[i][j] <= 100
Hints for Rotate Image
Hint 1
Rotated[i][j] comes from matrix[n-1-j][i].
Hint 2
In place: transpose across the main diagonal, then reverse every row.
How to solve Rotate Image
A 90-degree clockwise rotation sends the value at matrix[n-1-j][i] to position [i][j] — the bottom-left corner becomes the top-left. The memorable in-place recipe is transpose, then reverse each row, which composes exactly into that mapping.
Approach
- Read
n, the side length of the square matrix. - Direct construction: set
out[i][j] = matrix[n - 1 - j][i]for every cell. - In-place alternative: swap
matrix[i][j]withmatrix[j][i]forj > i— a transpose across the main diagonal. - Then reverse each row.
- Both produce the same result; the second uses no extra matrix.
Why it works
Transposing maps (i, j) to (j, i), a reflection across the main diagonal. Reversing each row then maps (i, j) to (i, n-1-j), a horizontal flip. Composing them sends the original (i, j) to (j, n-1-i) — precisely a 90-degree clockwise turn. Two reflections about intersecting axes always compose into a rotation, which is why this trick works.
Complexity
- Time —
O(n^2) - Space —
O(1) in place, or O(n^2) building a new matrix
Pitfalls
- When transposing in place, iterate only the upper triangle (
j > i). Covering all pairs swaps everything twice and leaves the matrix unchanged. - The order matters: transpose then reverse rows gives clockwise; reversing first gives counter-clockwise.
- The direct formula is
matrix[n - 1 - j][i], easy to confuse withmatrix[j][n - 1 - i](which rotates the other way). - This only works because the matrix is square.
Reference solution
Python
from typing import List
def rotate(matrix: List[List[int]]) -> List[List[int]]:
n = len(matrix)
return [[matrix[n - 1 - j][i] for j in range(n)] for i in range(n)]Java
public static int[][] rotate(int[][] matrix) {
int n = matrix.length;
int[][] out = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) out[i][j] = matrix[n - 1 - j][i];
}
return out;
}C++
vector<vector<int>> rotate(vector<vector<int>>& matrix) {
int n = (int) matrix.size();
vector<vector<int>> out(n, vector<int>(n, 0));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) out[i][j] = matrix[n - 1 - j][i];
}
return out;
}JavaScript
var rotate = function(matrix) {
const n = matrix.length;
const out = [];
for (let i = 0; i < n; i++) {
const row = [];
for (let j = 0; j < n; j++) row.push(matrix[n - 1 - j][i]);
out.push(row);
}
return out;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 1199 arrays problems · the whole catalogue
Learn the technique: Arrays · Matrix and Grid Traversal