- Interview Questions
- /
- Problems
- /
- Rotate Image
Rotate Image
Turn a square grid 90 degrees clockwise without allocating a second grid. The trick is that a rotation is a transpose followed by a mirror. Solve it below in Python or JavaScript, then read the walkthrough.
Problem
You're given an n × n grid of integers matrix. Rotate it 90 degrees clockwise, in place: modify matrix directly instead of building and returning a new grid.
After the rotation, the first row should hold what used to be the first column, read from bottom to top.
Your function doesn't need to return anything. The grid you were given is checked after the call.
Example 1
- Input
- matrix = [[1, 2], [3, 4]]
- Output
- [[3, 1], [4, 2]]
The left column (1, 3), read bottom to top, becomes the top row.
Example 2
- Input
- matrix = [[5, 8, 2], [0, 7, 4], [9, 6, 1]]
- Output
- [[9, 0, 5], [6, 7, 8], [1, 4, 2]]
The center value 7 stays put; every other value moves a quarter turn.
Example 3
- Input
- matrix = [[7]]
- Output
- [[7]]
Constraints
- n == matrix.length == matrix[i].length
- 1 ≤ n ≤ 20
- -1000 ≤ matrix[i][j] ≤ 1000
Practice
Runs 9 tests in your browser. Python loads on the first run (about 6 MB, then cached). Press ⌘/Ctrl + Enter to run.
Get asked Rotate Image in a real interview?
Interview Solver gives you a working solution live, during your actual coding interview. Invisible to screensharing.
How to solve Rotate Image
Pattern: Transpose, then reverse each row
Track where one cell goes. A clockwise quarter turn moves the value at row r, column c to row c, column n - 1 - r.
That move can be split into two simpler ones that are each easy to do in place. Transposing (swapping across the main diagonal) sends (r, c) to (c, r). Reversing each row then sends (c, r) to (c, n - 1 - r). Together they give exactly the rotation. Both steps are plain swaps, so no second grid is needed.
Approach 1: Copy into a new grid
Allocate n × n and write rotated[c][n - 1 - r] = matrix[r][c], then copy it back. Straightforward, but it uses O(n²) extra memory, which the in-place requirement rules out.
Time O(n²) · Space O(n²)
Approach 2: Rotate four cells at a time, ring by ring
For each layer from the outside in, every cell on the top edge belongs to a cycle of four cells (top, right, bottom, left). Save one, shift the other three around, and drop the saved value in the last spot. In place and optimal, but the index arithmetic is easy to get wrong under pressure.
Time O(n²) · Space O(1)
Approach 3: Transpose, then reverse each row
- For every
r < c, swapmatrix[r][c]withmatrix[c][r]. Only visit the upper triangle, or you'll swap each pair twice and undo the work. - Reverse every row in place.
Each cell is touched a constant number of times.
Time O(n²) · Space O(1)
| Approach | Time | Space |
|---|---|---|
| Copy into a new grid | O(n²) | O(n²) |
| Rotate four cells at a time, ring by ring | O(n²) | O(1) |
| Transpose, then reverse each row | O(n²) | O(1) |
Solution code
Try the practice editor first. Both solutions pass every test above.
Rotate Image solution in Python
def rotate(matrix: list[list[int]]) -> None:
n = len(matrix)
for r in range(n):
for c in range(r + 1, n):
matrix[r][c], matrix[c][r] = matrix[c][r], matrix[r][c]
for row in matrix:
row.reverse()Rotate Image solution in JavaScript
function rotate(matrix) {
const n = matrix.length;
for (let r = 0; r < n; r++) {
for (let c = r + 1; c < n; c++) {
[matrix[r][c], matrix[c][r]] = [matrix[c][r], matrix[r][c]];
}
}
for (const row of matrix) row.reverse();
}Common mistakes
- Looping the transpose over every `(r, c)` instead of only `r < c`, which swaps each pair back.
- Reversing the columns (or the row order) instead of each row, which produces a counterclockwise rotation.
- Building a new grid and reassigning the parameter (`matrix = rotated`). The caller still holds the old grid, so nothing changes.
- In the four-way swap, mixing up `n - 1 - i` and `n - 1 - layer` and corrupting a ring.
What interviewers look for
Start by writing the mapping (r, c) → (c, n - 1 - r) and checking it on a corner. Then say how it decomposes into a transpose and a row reversal. Interviewers like that derivation because it shows you didn't just memorize the trick.
If you go with the four-way swap instead, name your loop bounds before coding (layers 0..n/2, offsets within the layer) and trace one 3 × 3 ring. Either way, confirm you're mutating the input and not returning a copy.
Follow-up questions
How would you rotate counterclockwise?
Transpose, then reverse each column (or equivalently reverse each row first, then transpose). The cell mapping is `(r, c) → (n - 1 - c, r)`.
What about rotating 180 degrees?
Reverse the order of the rows and then reverse each row. That is the same as two quarter turns but needs no transpose.
What if the matrix is not square?
An m × n grid becomes n × m, so it cannot be rotated in the same storage with simple swaps. Allocate the new shape and use `rotated[c][m - 1 - r] = matrix[r][c]`; in-place versions on a flat buffer need cycle-following and are rarely worth it.
Companies that ask Rotate Image
Rotate Image is tagged by 133 companies in our interview dataset. The score shows how often it comes up at each company.
- Cabaletta Bio100%
- Clear Street100%
- Zillow100%
- Just Eat90%
- Saic90%
- Rakuten88%
- Susquehanna International Group86.7%
- Discord84%
- Cox Automotive84%
- Cisco83.5%
- Check Point Software Technologies83%
- Biontech79%
- IBM78.8%
- Red Hat77%
- Citigroup76.9%
- Jetbrains76%
- Zoho75.7%
- Qualcomm75.6%
- Tata Consultancy Services75%
- Shutterfly75%
- Ceridian75%
- Vistaprint75%
- Mastercard75%
- Gopuff74%
and 109 more.
Practice next
Get live help in the real interview
Working solutions in real time during your actual interview. Invisible to screensharing.
Start Free TrialNo credit card required