Rotate Image
Problem Statement
You are given an n x n two-dimensional matrix representing an image. Rotate the image by 90 degrees clockwise in place.
Input
A square integer matrix matrix with n rows and n columns.
Output
No value is returned. The same matrix is mutated so it represents the image rotated 90 degrees clockwise.
Constraints
- •
1 <= n <= 20 - •
-1000 <= matrix[i][j] <= 1000 - •
The matrix must be rotated in place without allocating another matrix
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]]Learning Objectives
- Map a clockwise rotation from coordinate movement to simpler matrix operations.
- Use transposition to swap values across the main diagonal in place.
- Reverse each row after transposition to complete the clockwise rotation.
- Contrast transpose-and-reverse with the equivalent four-way cycle swap.
Intuition
Pattern Recognition
A new matrix would make the coordinate rule easy: old row, col moves to new col, n - 1 - row. But the prompt requires in-place mutation, so writing directly to target cells would overwrite values that still need to move.
The clean in-place transformation breaks the rotation into two reversible steps. First transpose the matrix across its main diagonal, turning rows into columns. Then reverse each row, which moves those columns into clockwise order. A layer-by-layer four-way cycle implements the same mapping, but transpose then reverse is usually easier to explain and less error-prone.
Common mistakes
- ×Allocating a second matrix even though the problem requires in-place rotation.
- ×Transposing the full matrix twice by swapping both **row, col** and **col, row**.
- ×Reversing columns instead of rows after transposing, which rotates counterclockwise.
- ×Trying to write every value to its final coordinate directly and overwriting needed values.
Algorithm Explanation
Key idea
A 90-degree clockwise rotation equals transpose across the main diagonal, then reverse each row. Transpose changes matrix[row][col] with matrix[col][row] for cells above the diagonal. Row reversal then puts each transposed row in clockwise order.
Walkthrough
For matrix = [[1,2,3],[4,5,6],[7,8,9]], transposition swaps 2 with 4, 3 with 7, and 6 with 8. The matrix becomes [[1,4,7],[2,5,8],[3,6,9]]. Reversing each row gives [[7,4,1],[8,5,2],[9,6,3]], which is the clockwise rotation.
The same final movement can also be done with a four-way cycle around each layer: top takes left, left takes bottom, bottom takes right, and right takes saved top. In interviews, transpose then reverse is shorter to code and easier to verify.
Algorithm
- Let n be the matrix size.
- For every row, swap cells above the diagonal: columns row + 1 through n - 1.
- After transposition, iterate through each row.
- Reverse that row in place with two pointers left and right.
- The matrix is now rotated 90 degrees clockwise.
Solutions
Solution: Transpose then reverse rows
Transposition performs the diagonal swaps without extra storage, and reversing each row completes the clockwise coordinate mapping. Both operations are in-place and deterministic.
Step-by-step
- Loop over the upper triangle of the matrix and swap each value with its mirrored value across the main diagonal.
- For every row, place two pointers at the left and right ends.
- Swap the row endpoints and move both pointers inward until the row is reversed.
- After all rows are reversed, the matrix has been rotated clockwise.
O(n^2)
O(1)
Every matrix cell participates in at most a constant number of swaps and no extra matrix is allocated.
Java implementation
Dry Run
Sample input
matrix = [[1,2,3],[4,5,6],[7,8,9]]. Track the transpose swaps and row reversals.
| phase | operation | matrix state |
|---|---|---|
| start | original matrix | [[1,2,3],[4,5,6],[7,8,9]] |
| transpose | swap 2 with 4 | [[1,4,3],[2,5,6],[7,8,9]] |
| transpose | swap 3 with 7 and 6 with 8 | [[1,4,7],[2,5,8],[3,6,9]] |
| reverse rows | reverse row 0 | [[7,4,1],[2,5,8],[3,6,9]] |
| reverse rows | reverse row 1 | [[7,4,1],[8,5,2],[3,6,9]] |
| reverse rows | reverse row 2 | [[7,4,1],[8,5,2],[9,6,3]] |
Transposition turns columns into rows, and reversing each row changes the order from left-to-right into the clockwise orientation.
Interview Tips
State the coordinate mapping first, then say you will implement that mapping through transpose plus row reversal. This reassures the interviewer that the trick is not memorized magic. If asked for an alternative, describe the layer-by-layer four-way cycle, but code the transpose version unless specifically requested.
Likely follow-ups
- How would you rotate the matrix 90 degrees counterclockwise?
- How would you rotate by 180 degrees in place?
- Can this exact in-place method work for a non-square matrix?
- How would you implement the layer-by-layer four-way cycle instead?
Similar Problems
Key Takeaways
- Clockwise rotation maps **row, col** to **col, n - 1 - row**.
- Transpose then reverse each row implements that mapping in place.
- Only swap the upper triangle during transpose to avoid undoing swaps.
- A four-way layer cycle is equivalent but easier to get wrong under pressure.