Quarter-Turn a Square Grid of Pixels Without Extra Storage
Arrays & HashingAuto-graded · 17 test casesLast updated
Turn a square integer grid a quarter turn clockwise by modifying it in place, testing index mapping and in-place transforms such as transpose plus row reversal.
You are given a square grid of integers with n rows and n columns, where each cell holds a pixel value. Turn the grid a quarter turn (90 degrees) clockwise.
The rotation must be done in place: change the cells of the grid you were given, and do not build a second n x n grid to hold the result. After the rotation, return the same grid object.
Under a clockwise quarter turn, the value at row r, column c of the original grid ends up at row c, column n - 1 - r. The first row of the original becomes the last column of the result, the second row becomes the second-to-last column, and so on.
Input
grid: a list ofnlists, each containingnintegers.
Output
- The rotated grid, as a list of lists of integers. This is the same object that was passed in, modified in place.
Constraints
gridis square:len(grid) == len(grid[i]) == nfor every rowi1 <= n <= 500-1000 <= grid[i][j] <= 1000- Values may repeat, and may be negative
- A
1 x 1grid is returned unchanged
Examples
Example 1
Input: rotate_grid_clockwise([[1, 2], [3, 4]])
Output: [[3, 1], [4, 2]]
Example 2
Input: rotate_grid_clockwise([[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
Work out where a single cell goes. The cell at (r, c) must end at (c, n - 1 - r). Check this on the 2 x 2 case by hand.
Hint 2
Try splitting the quarter turn into two simpler in-place moves. One of them swaps cells across the main diagonal.
Hint 3
After swapping across the main diagonal, each row still needs one more fix. Think about what happens to column positions when you reverse a row. Be careful to swap each off-diagonal pair only once.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
A clockwise quarter turn sends the value at (r, c) to (c, n - 1 - r). Moving values directly to those targets means juggling cycles of four cells. It is simpler to split the move into two easy in-place steps:
- Transpose. Swap
(r, c)with(c, r)for every pair above the main diagonal. The value at(r, c)moves to(c, r). - Reverse every row. In row
c, the value in columnrmoves to columnn - 1 - r. So(c, r)becomes(c, n - 1 - r).
Chained together, (r, c) goes to (c, r) and then to (c, n - 1 - r), which is exactly the required mapping.
Why it is correct
- The transpose loop only visits pairs with
c > r, so each off-diagonal pair is swapped once. Visiting all pairs would swap each twice and undo the work. Diagonal cells already sit at(r, r)and stay put. - Reversing a row only moves values within that row, so each row's contents stay in the right row.
- The composition of the two steps equals the rotation mapping for every cell, so every value lands in its correct place.
Check with [[1,2],[3,4]]: the transpose gives [[1,3],[2,4]], and reversing each row gives [[3,1],[4,2]], as expected.
Edge cases
n = 1: both loops do nothing, so the grid is unchanged.- Odd
n: the center cell is on the diagonal and in the middle of its row, so it stays fixed. - Negative or repeated values do not matter, since only positions change.
Complexity
- Time: O(n^2). The transpose handles about n^2 / 2 pairs and the row reversals touch n^2 cells.
- Space: O(1) extra. All swaps and reversals happen in place.
Reference solution
from typing import List
def rotate_grid_clockwise(grid: List[List[int]]) -> List[List[int]]:
n = len(grid)
# Transpose: swap each pair across the main diagonal exactly once
for r in range(n):
for c in range(r + 1, n):
grid[r][c], grid[c][r] = grid[c][r], grid[r][c]
# Mirror each row left to right
for row in grid:
row.reverse()
return grid