Skip to content

Practice Rotate Image

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#48
LeetCode Problem

Rotate Image

Problem Statement

You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise). You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.

Source: Rotate Image on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output
[[7,4,1],[8,5,2],[9,6,3]]

# Example 2

Input
matrix = [[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]]

Constraints

n == matrix.length == matrix[i].length
1 <= n <= 20
-1000 <= matrix[i][j] <= 1000

How to solve Rotate Image

Original notes for interview preparation, written for this problem. Reviewed .

Key insight

A clockwise quarter turn sends the element at row r, column c to row c, column n - 1 - r, and that mapping is exactly a transpose across the main diagonal followed by reversing each row, both of which are simple in-place swaps.

Why brute force is not enough

Allocate a second n by n matrix, write each element to its rotated position, and copy it back. That is O(n^2) time but O(n^2) extra space, which the problem explicitly forbids.

Approach

  1. Transpose in place: for every pair of indices with row strictly less than column, swap the element at (row, column) with the element at (column, row). Visiting only the upper triangle ensures each pair is swapped exactly once.
  2. After the transpose, the element originally at (r, c) sits at (c, r).
  3. Reverse each row in place, which moves column index r to column n - 1 - r, so the element now sits at (c, n - 1 - r), the rotated position.
  4. Each step is a bijection on positions, so no value is overwritten before it is moved; this is the invariant that makes constant extra space possible. For a counterclockwise turn, transpose and then reverse each column instead.

Walkthrough of Example 1

matrix = [[1,2,3],[4,5,6],[7,8,9]] → [[7,4,1],[8,5,2],[9,6,3]]

  1. Transpose: swap 2 with 4, 3 with 7, and 6 with 8. The matrix becomes [[1,4,7],[2,5,8],[3,6,9]].
  2. Reverse row 0 to get [7,4,1], row 1 to get [8,5,2], and row 2 to get [9,6,3].
  3. The matrix is now [[7,4,1],[8,5,2],[9,6,3]], matching the expected output; for instance the original 1 at (0,0) ended at (0,2).

Time and space

Time O(n^2) · Space O(1). The transpose touches about n^2 / 2 pairs and the row reversals touch every element once more, where n is the side length; only a temporary for swapping is used.

Interview pitfalls

  • Transposing over all (row, column) pairs rather than only row < column, which swaps each pair twice and leaves the matrix unchanged.
  • Reversing columns instead of rows after the transpose, or reversing rows before transposing, which produces a counterclockwise rotation or a reflection.
  • Mixing up the target formula: clockwise sends (r, c) to (c, n - 1 - r); writing (n - 1 - c, r) rotates the other way.
  • In the layer-by-layer four-way swap, miscounting the elements per ring (n - 1 - 2 x layer) so corners are rotated twice or skipped.
  • Assuming the transpose trick works for rectangular matrices; it relies on the matrix being square, which the constraints guarantee here.

Follow-up questions interviewers ask

  • How would you rotate by 180 or 270 degrees, or counterclockwise, using the same two primitives?
  • How would you rotate a non-square m by n matrix, given that an in-place transpose is not available?
  • Can you rotate with a single pass of four-way swaps ring by ring, and how does it compare in clarity and cache behavior?

Further reading on the technique: Transpose

Related LeetCode Problems

Practice more problems similar to Rotate Image.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.