#48 Rotate Image
frequently reported atMicrosoftAppleBloombergAmazonGoogleMeta
the problem
In plain words
Rotate an n × n matrix 90° clockwise, in place — do not allocate another matrix.
in matrix = [[1,2,3],[4,5,6],[7,8,9]]
out [[7,4,1],[8,5,2],[9,6,3]]
in matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
out [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]
- n == matrix.length == matrix[i].length
- 1 ≤ n ≤ 20
- -1000 ≤ matrix[i][j] ≤ 1000
your turn
Try it before you peek
Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.
Solve it in the browser
LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.
Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.
Marking the problem solved, or opening every hint, also shows it.
the aha
Key insight
patternMatrix traversal & in-place transforms
A clockwise rotation sends (r, c) to (c, n−1−r). That's two easy moves: a transpose (r, c) → (c, r), then a mirror of each row.
solutions
From brute force to optimal
Write each cell to its rotated position in a fresh matrix, then copy back.
- rotated[c][n−1−r] = matrix[r][c] for every cell.
- Copy rows back into matrix.
Every cell is moved once.
A full second matrix — breaks the in-place rule.
class Solution { public void rotate(int[][] matrix) { int n = matrix.length; int[][] rotated = new int[n][n]; for (int r = 0; r < n; r++) for (int c = 0; c < n; c++) rotated[c][n - 1 - r] = matrix[r][c]; // row r becomes column n-1-r for (int r = 0; r < n; r++) matrix[r] = rotated[r]; }}Mirror across the main diagonal, then mirror each row left-to-right.
- For r < c swap matrix[r][c] and matrix[c][r].
- Reverse every row with two pointers.
Each cell is swapped at most twice.
One temp variable.
class Solution { public void rotate(int[][] matrix) { int n = matrix.length; for (int r = 0; r < n; r++) { // step 1: transpose (mirror over the diagonal) for (int c = r + 1; c < n; c++) { int tmp = matrix[r][c]; matrix[r][c] = matrix[c][r]; matrix[c][r] = tmp; } } for (int[] row : matrix) { // step 2: reverse every row for (int left = 0, right = n - 1; left < right; left++, right--) { int tmp = row[left]; row[left] = row[right]; row[right] = tmp; } } }}Dry run
Each ring rotates as groups of four cells: left → top → right → bottom → left.
- For each layer from the outside in.
- For each position in the layer, cycle its four cells with one temp.
Every cell is moved exactly once.
One temp variable.
class Solution { public void rotate(int[][] m) { int n = m.length; for (int layer = 0; layer < n / 2; layer++) { int first = layer, last = n - 1 - layer; for (int i = first; i < last; i++) { int offset = i - first; int top = m[first][i]; m[first][i] = m[last - offset][first]; // left -> top m[last - offset][first] = m[last][last - offset]; // bottom -> left m[last][last - offset] = m[i][last]; // right -> bottom m[i][last] = top; // top -> right } } }}in the room
How to say it in the interview
talk track
- Write the mapping (r, c) → (c, n−1−r) first; it justifies every approach.
- Then: "a transpose plus a row reverse produces exactly that mapping." Verify on the 3×3 example.
traps
- Transposing with c starting at 0 — every pair is swapped twice and nothing changes.
- Reversing columns instead of rows (that gives a counter-clockwise rotation).
what they ask next
Follow-ups
Counter-clockwise?
Transpose, then reverse each column (or reverse rows first, then transpose).
before you submit
Edge cases to test
- 1×1 → nothing changes.
- 2×2: [[1,2],[3,4]] → [[3,1],[4,2]].
- Odd n: the centre cell never moves.
practise next
Similar problems
- #54Spiral MatrixMediumin Matrices
- #189Rotate ArrayMediumin Arrays & Strings