Rotate a Square Matrix 90° Clockwise In Place
Arrays & HashingAuto-graded · 16 test casesLast updated
Tests whether a candidate can transform an n×n grid in place using transpose and row-reversal, with no extra matrix allocated.
Rotate a Square Matrix 90° Clockwise In Place
You are given an n × n grid of integers. Rotate the entire grid a quarter-turn clockwise, modifying it directly without allocating a second matrix. Return the modified grid.
A 90° clockwise rotation moves every cell at row i, column j to row j, column n − 1 − i.
For example, the top-left corner ends up at the top-right, and the bottom-left corner ends up at the top-left.
What "in place" means here
You may use a constant amount of extra memory (a few temporary variables for swaps). You may not create a full copy of the matrix or any structure proportional to n².
Constraints
1 ≤ n ≤ 20- The matrix is always square (
nrows, each with exactlynelements). -1000 ≤ matrix[i][j] ≤ 1000- The input is modified directly; the same object must be returned.
Examples
Example 1
Input: rotate_grid([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
Output: [[7, 4, 1], [8, 5, 2], [9, 6, 3]]
Example 2
Input: rotate_grid([[5, 1, 9, 11], [2, 4, 8, 10], [13, 3, 6, 7], [15, 14, 12, 16]])
Output: [[15, 13, 2, 5], [14, 3, 4, 1], [12, 6, 8, 9], [16, 7, 10, 11]]
Hints
Hint 1
A 90° clockwise rotation can be decomposed into two simpler operations that each only touch pairs of elements. What two things happen to a matrix when you rotate it?
Hint 2
Try transposing first: swap every element at position (i, j) with the element at (j, i), but only for pairs where j > i so you don't swap twice. After that, look at what each row needs to become.
Hint 3
After the transpose, each row is the mirror image of what it should be. Reversing every row in place finishes the job — and both steps together use only O(1) extra space.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
A 90° clockwise rotation can be broken into two steps that are each trivial to implement in place.
Step 1 — Transpose. Swap every element matrix[i][j] with matrix[j][i], but only when j > i. The condition j > i ensures each pair is swapped exactly once. After this step, the grid is reflected along its main diagonal.
Step 2 — Reverse each row. Calling .reverse() on every row costs O(n) per row and needs no extra memory. Python's list.reverse is in place.
Why the two steps compose to a clockwise rotation
A clockwise rotation maps (i, j) to (j, n−1−i).
- The transpose maps
(i, j)to(j, i). - The row reversal maps
(j, i)to(j, n−1−i).
Applying them in sequence lands each element exactly where a clockwise rotation requires.
Edge cases
- 1×1 grid: the inner loops never execute and the row reversal is a no-op. Correct.
- All identical values: no observable change, still correct.
- Negative values: the algorithm is purely positional; values don't matter.
Complexity
| Time | O(n²) — every cell is touched a constant number of times across both steps |
| Space | O(1) extra — only a handful of swap variables are allocated |
Full solution
from typing import List
def rotate_grid(matrix: List[List[int]]) -> List[List[int]]:
n = len(matrix)
# Step 1: transpose — swap matrix[i][j] with matrix[j][i] for j > i
for i in range(n):
for j in range(i + 1, n):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
# Step 2: reverse each row
for row in matrix:
row.reverse()
return matrix