Skip to content
AI360Xpert
Beta
Difficulty: MediumMath & Geometry

Rotate Image

Problem in Plain English

Rotate an n x n matrix 90 degrees clockwise in place, rewriting the input directly instead of allocating a second matrix.

Problem Statement

You are given an n x n 2D matrix picturing an image. Rotate the image 90 degrees clockwise.

The rotation must happen in place: modify the input matrix directly without allocating another 2D matrix for the result.

Constraints

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

Examples

Example 1

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

Each row becomes a column read bottom to top: the top row [1, 2, 3] becomes the right column.

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]]

The old left column [5, 2, 13, 15] becomes the new top row [15, 13, 2, 5]. The inner 2 by 2 block also rotates: [[4, 8], [3, 6]] becomes [[3, 4], [6, 8]], so rotating just the outer border would be wrong.

Example 3

Input
matrix = [[-1000]]
Output
[[-1000]]

The only cell is also the center. Both diagonal swapping and row reversal have no pairs to process, so its value and position stay unchanged.

Intuition

Follow positions rather than values: a clockwise quarter-turn sends row r, column c to row c, column n - 1 - r. In Example 1, the top-left 1 becomes the top-right 1, and the bottom-left 7 becomes the top-left 7. Directly overwriting destinations would destroy values still needed elsewhere. A spare matrix illustrates the mapping, but violates the storage requirement. Instead, swap across the main diagonal and then reverse each row; these two reflections achieve the same movement without losing a value.

Approaches

Copy to New Matrix

Solution Details

Reveal Copy to New Matrix: intuition, complexity, and code

Hints

Hint 1
Follow one cell: 1 starts at the top-left and must end at the top-right, so rows become columns.
Hint 2
Try transposing first (swap matrix[i][j] with matrix[j][i] for j above the diagonal) and look at what each row needs next.
Hint 3
After the transpose, reversing every row left to right completes the clockwise turn with no extra matrix.

Edge Cases

  • A 1 by 1 matrix such as [[-1000]] has no swap pairs and remains unchanged.
  • A 2 by 2 matrix [[1, 2], [3, 4]] becomes [[3, 1], [4, 2]]: one transpose swap and one reversal swap per row.
  • Odd sizes keep the central cell fixed; other cells in its row and column still move.
  • Even sizes have no single central cell; inner layers must rotate as well as the border, as in Example 2.
  • Repeated values, zeros, and values -1000 or 1000 are handled by position, not by comparison.

Common Mistakes and Interview Tips

  • Transposing with j starting at 0 swaps every off-diagonal pair twice. Start at i + 1.
  • Overwriting matrix[c][n - 1 - r] directly during a source scan destroys unread values; use swaps for the actual in-place solution.
  • Reversing columns after transposition produces a counterclockwise rotation. Reverse each row for clockwise movement.
  • Reversing through the entire row swaps each pair twice. Stop before half the length.
  • Rebinding matrix to a new result only changes a local variable in Python/JavaScript; it does not update the caller matrix. Replacing rows also loses row identity.
  • The copy baseline shows the coordinate mapping, but a second matrix still violates the stated restriction even if copied back. In an interview, derive the two reflections and explain why each pair is visited once.

Key Takeaway

A rotation can factor into simpler reflections: transpose plus row reversal turns clockwise, and each factor needs only pairwise swaps.