Rotating a 2D matrix
Lesson 7 of 11
7Rotating a 2D matrix
The big pictureEvery grid problem starts with one question: if I hold one element, where does it go? Write the coordinates before and after for small examples, the rule appears, and the code becomes its translation.
What the picture shows: Two simple steps give the rotation: the transpose flips the grid over its diagonal, and the reverse completes the quarter turn.
Map of this part
The concepts in order, click any one to jump to it:
Two ways into the same idea
Take a printed square photo and turn it a quarter clockwise. The top row becomes the right column, and the left column becomes the top row. The program does the same cell by cell; we only need a rule that says where each cell goes.
For a clockwise turn: new[c][n-1-r] = old[r][c]. With a new copy the solution is O(n²) time and memory. In place: the transpose swaps m[r][c] with m[c][r] for cells above the diagonal only, then row.reverse() on every row, giving O(n²) time and O(1) extra memory. Another option: rotate the rings from outside in, four cells at a time.
Before You Start
🐍Nested lists
You know m[r][c] and nested loops.
The Core Idea
1Derive the rule from an example
In a 3×3 grid, the 1 at (0,0) moves to (0,2), and the 2 at (0,1) moves to (1,2). The old row became the column, and the old column became n-1-row.
def rotated_copy(m):
n = len(m)
out = [[None] * n for _ in range(n)]
for r in range(n):
for c in range(n):
out[c][n - 1 - r] = m[r][c]
return out
grid = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
for row in rotated_copy(grid):
print(row)2Transpose, then reverse
The transpose flips the grid over its main diagonal. Apply it and then reverse each row and you get the same result. Try the two steps separately to see each one:
grid = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
transposed = [list(col) for col in zip(*grid)]
print("transpose:", transposed)
turned = [row[::-1] for row in transposed]
print("reversed rows:", turned)c > r.3Exercise: in place
The common interview ask: change the same list without building a new grid. Use an in-place transpose by swapping m[r][c], m[c][r] above the diagonal, then reverse() each row. Check with a test: four rotations bring back the original grid.
Worked example
Problem: a 2×2 image [[1,2],[3,4]]. Analysis: the transpose is [[1,3],[2,4]], then reversing each row gives [[3,1],[4,2]]. Check: the 1 was top-left and is now top-right, which is a quarter turn clockwise.
Myth vs Reality
| Myth | Reality |
|---|---|
| You always need a new grid. | The transpose and reverse work in place with constant extra memory. |
| The solution works for any rectangle. | In-place rotation is for squares only. An m×n rectangle becomes n×m and needs a new grid. |
Where you meet it at work
🖼️Image processing
Rotating images and fixing camera orientation.
🎮Games
Rotating pieces in puzzle and grid games.
Test your understanding
In a 90° clockwise turn of an n×n grid, where does (r, c) go?
The row becomes the column, and the old column becomes n-1-row.
Why visit only c > r in an in-place transpose?
A double swap cancels out and the grid stays the same.
What is the extra memory of the in-place solution?
We only swap cells with no new grid.
Quick review cards
Check Your Understanding
How do you turn counter-clockwise?
Transpose, then reverse the order of the rows (not their contents).
Does it work on a 1×1 grid?
Yes, the transpose and the reverse change nothing.
What is the time?
O(n²) because each cell is touched once or twice.
Resources for this lesson
Grouped by type. Start with the official docs for precision, or an article or video for the explanation.
Done with this lesson?
Sign in to save your progress.