Rotate Image
Table of Contents + −
Rotate Image is a favorite because the easy answer wastes memory. Anyone can copy the grid into a new grid in rotated order. But the interviewer adds one rule. Do it in place. No second grid. That small rule turns a simple copy into a neat two-step trick. Showing that trick is the whole point.
🎯 The Problem
You get a square matrix and you must turn it in place. Here are the rules.
- The grid is square. The same number of rows and columns.
- Turn it 90 degrees clockwise. The top row swings to the right side.
- Change the grid in place. No second grid the same size.
- The first column read bottom to top becomes the new top row.
Look at this grid. After a clockwise turn, 7 4 1 becomes the new top row.
Input:1 2 34 5 67 8 9
Output:7 4 18 5 29 6 3
Explanation: turn the grid 90 degrees clockwise, in place.Here is what one rotation does to a single cell. The value at row 0 col 0 moves to row 0 col 2.
🐢 Approach 1: Copy Into a New Matrix (Brute Force)
The idea in one line: build a fresh grid, place each cell where the turn sends it, then copy it back.
The idea:
- Make a new grid the same size.
- Old cell
(i, j)lands at new cell(j, n - 1 - i). - Fill the new grid, then copy it over the old one.
Why it is weak:
- It needs a second grid as big as the input.
- That extra space is O(n²).
- The interviewer almost always bans the second grid.
Here is the extra-matrix code:
def rotate(matrix): n = len(matrix) copy = [[0] * n for _ in range(n)]
for r in range(n): for c in range(n): copy[c][n - 1 - r] = matrix[r][c]
for r in range(n): matrix[r][:] = copy[r]⚡ Approach 2: Transpose Then Reverse Each Row (Best)
The idea in one line: a clockwise turn is just a transpose, then a reverse of each row, both inside the same grid.
The idea:
- Transpose means flip the grid across its main diagonal.
- The main diagonal runs from top left to bottom right.
- Transpose swaps cell
(i, j)with cell(j, i). Rows become columns.
How it works:
- Transpose first. Rows and columns trade places.
- Each row is now in the wrong left-right order.
- Reverse each row. Swap first with last, second with second-last, and so on.
- Two flips together equal one clockwise turn.
Why it is fast:
- Both flips happen inside the same grid.
- The extra space is O(1).
- The order matters. Transpose first, then reverse rows.
Steps to Solve
- Transpose the matrix. For every pair
(i, j)withj > i, swapmatrix[i][j]andmatrix[j][i]. - Reverse each row. For each row, swap the cells from the two ends moving toward the middle.
- The matrix is now turned 90 degrees clockwise, all in place.
Here is the two-step flow on the example.
This Python version transposes with a swap loop, then reverses each row with slicing.
def rotate(matrix): n = len(matrix) for i in range(n): # transpose for j in range(i + 1, n): matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j] for row in matrix: # reverse each row in place row.reverse()
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]rotate(matrix)for row in matrix: print(" ".join(str(v) for v in row))The output of the above code will be:
7 4 18 5 29 6 3Let us read the Python version line by line, because the two steps must happen in this exact order.
n = len(matrix) records the side length. Since the matrix is square, the row count and column count are the same.
The transpose loop is the first key part. The outer loop runs i over every row. The inner loop runs j from i + 1 to the end. Starting j at i + 1 is on purpose. It only touches cells above the main diagonal. So each pair is swapped once, not twice. If j started at 0, we would swap every pair twice and end up back where we started. The line matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j] swaps the mirror cells across the diagonal.
After this loop the rows and columns have traded places. But the values inside each row are in the wrong order for a clockwise turn.
The second loop fixes that. for row in matrix: row.reverse() reverses each row end to end. row.reverse() flips the row in place, so it costs no extra grid. After both steps the matrix is turned 90 degrees clockwise.
The order matters. Transpose first, then reverse rows. Swap the order and you get a counter-clockwise turn instead.
⏱️ Time and Space Complexity
Both approaches touch each cell a constant number of times. So the time is O(n²) for an n by n grid. The difference is memory. The copy approach needs a second grid, so its space is O(n²). The transpose-then-reverse approach works inside the same grid, so its extra space is just O(1).
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Copy into a new matrix | O(n²) | O(n²) |
| Transpose then reverse rows | O(n²) | O(1) |
Tip
For a clockwise turn, transpose then reverse each row. For a counter-clockwise turn, transpose then reverse each column instead. Remembering both saves you in follow-up questions.
🧩 Key Takeaways
- ✅ A clockwise 90-degree turn equals a transpose followed by reversing each row.
- ✅ Transpose swaps cell (i, j) with cell (j, i), flipping across the main diagonal.
- ✅ Start the inner transpose loop at i + 1 so each pair is swapped only once.
- ✅ Both steps happen in place, so the extra space is O(1).
- ✅ The order matters: transpose first, then reverse rows, or you turn the wrong way.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
What two steps rotate a matrix 90 degrees clockwise in place?
Why: Transpose flips across the diagonal, then reversing each row completes the clockwise turn.
- 2
What does transposing a matrix do?
Why: Transpose mirrors across the main diagonal, swapping (i, j) with (j, i) so rows and columns trade places.
- 3
Why does the transpose loop start its inner index at i + 1?
Why: Swapping each pair twice would cancel out. Starting at i + 1 touches each pair exactly once.
- 4
What is the extra space used by the transpose-then-reverse approach?
Why: Both steps work inside the same grid using single-value swaps, so the extra space is constant.