Rotate Image
Your code isn't saved yet: refreshing or leaving the page resets it. Saved progress comes with user accounts.
You're given an n x n matrix representing an image. Rotate it 90 degrees clockwise, in place: modify matrix directly, without allocating another matrix. The function returns nothing; the tests check matrix after it runs.
Understand the problem
- A matrix here is an array of rows:
matrix[r][c]is the value in rowr, columnc. - The matrix is square (
n x n). - Rotating 90° clockwise turns the picture a quarter turn to the right: the top row becomes the right column, the left column becomes the top row, and so on.
- In place means you change the given matrix itself. You can use a few variables, but not a second matrix.
Example 1
Input: Result:
1 2 3 7 4 1
4 5 6 → 8 5 2
7 8 9 9 6 3The top row 1 2 3 is now the right column (read top to bottom).
Example 2
Input: Result:
1 2 3 1
3 4 → 4 2Example 3
Input: [[5]]
Result: [[5]]A single cell doesn't change.
Constraints
1 <= n <= 20-1000 <= matrix[i][j] <= 1000
Hints
Stuck? Open one hint at a time. Each one gives away a little more than the last.
The idea in one sentence
A clockwise rotation is the same as flipping the matrix along its diagonal (transpose) and then reversing each row, and both of those are easy to do in place.
The naive approach
Create a new n x n matrix and copy each cell (r, c) to its new place (c, n - 1 - r). That's easy, but it uses a second matrix, which the problem forbids.
Moving cells one at a time inside the same matrix is hard, because each move overwrites a value you haven't moved yet.
The key insight: two simple moves
A 90° clockwise rotation can be split into two moves:
- Transpose: flip along the main diagonal (top-left to bottom-right). Every
matrix[r][c]swaps withmatrix[c][r], so rows become columns. - Reverse each row: mirror the matrix left to right.
1 2 3 transpose 1 4 7 reverse rows 7 4 1
4 5 6 → 2 5 8 → 8 5 2
7 8 9 3 6 9 9 6 3Why it works: a clockwise rotation sends the cell at (r, c) to (c, n - 1 - r). The transpose moves it to (c, r). Reversing the row moves column r to column n - 1 - r, giving (c, n - 1 - r). ✓
Both moves are just swaps, so they need no extra matrix.
Step by step
The transpose only swaps cells above the diagonal with their mirror below it (c > r):
| Swap | Values | Matrix after |
|---|---|---|
(0,1) ↔ (1,0) | 2 ↔ 4 | [1 4 3] [2 5 6] [7 8 9] |
(0,2) ↔ (2,0) | 3 ↔ 7 | [1 4 7] [2 5 6] [3 8 9] |
(1,2) ↔ (2,1) | 6 ↔ 8 | [1 4 7] [2 5 8] [3 6 9] |
Then reverse each row: [7 4 1] [8 5 2] [9 6 3]. Done.
The code
The reference solution is below, with comments on every important line.
How fast is it?
- Time: O(n²). Every cell is touched a constant number of times.
- Memory: O(1). Only swaps.
Common mistakes
- Swapping every pair twice in the transpose (looping
cfrom0), which undoes it. - Reversing the columns instead of the rows (or reversing first, then transposing). That gives a counter-clockwise rotation.
- Returning a new matrix. The tests check the original
matrix.
Going deeper
- Counter-clockwise: reverse each row first, then transpose.
- 180°: reverse each row and reverse the order of the rows.
- Another way: rotate in layers, moving four matching cells (top → right → bottom → left) with one temporary variable. Same speed, but the indexes are much easier to get wrong.
Reference solution
/**
* @param {number[][]} matrix
* @return {void}
*/
export default function rotate(matrix) {
const n = matrix.length
// 1. Transpose: swap each cell above the diagonal with its mirror below it.
// j starts at i + 1 so each pair is swapped once (twice would undo it).
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]]
}
}
// 2. Reverse each row. Together: (r, c) ends up at (c, n - 1 - r), a clockwise turn.
for (const row of matrix) row.reverse()
}export default function rotate(matrix: number[][]): void {
const n = matrix.length
// 1. Transpose: swap each cell above the diagonal with its mirror below it.
// j starts at i + 1 so each pair is swapped once (twice would undo it).
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]]
}
}
// 2. Reverse each row. Together: (r, c) ends up at (c, n - 1 - r), a clockwise turn.
for (const row of matrix) row.reverse()
}def rotate(matrix: list[list[int]]) -> None:
n = len(matrix)
# 1. Transpose: swap each cell above the diagonal with its mirror below it.
# j starts at i + 1 so each pair is swapped once (twice would undo it).
for i in range(n):
for j in range(i + 1, n):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
# 2. Reverse each row. Together: (r, c) ends up at (c, n - 1 - r), a clockwise turn.
for row in matrix:
row.reverse()