Rotate Image: Optimal In-Place Solution (Data-Backed Guide)

By DSA Prep Team · March 4, 2026 · 8 min read · Algorithms

Rotate Image is the #19 most asked LeetCode problem globally — the classic matrix transformation problem that tests in-place manipulation and geometric intuition.

Our analysis of 10,385 verified interview questions across 259 companies shows it appeared 60 times across 20 companies — including Google, Amazon, Meta, Microsoft, Goldman Sachs, Bloomberg, DE Shaw, J.P. Morgan, Capital One, and Mastercard.

The key insight is a two-step decomposition: transpose the matrix (swap matrix[i][j] with matrix[j][i]), then reverse each row. These two operations combine to produce a 90° clockwise rotation — and both can be done in-place in O(n²) time and O(1) space.

Table of Contents

The Data: 20 Companies Ask This

MetricValue
Total Interview Appearances60 verified mentions
Companies That Ask It20 companies
Global Rank#19 out of 10,385 questions
DifficultyMedium
Core PatternsArray, Math, Matrix

📌 Companies That Ask This Problem

Google, Amazon, Meta, Microsoft, Goldman Sachs, Bloomberg, DE Shaw, J.P. Morgan, Capital One, Mastercard, AMD, Accenture, Adobe, Apple, Cisco, ConsultAdd, Flipkart, IBM, Infosys, and Intel.

Problem Statement

LeetCode #48 — Rotate Image

You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees clockwise. You must rotate the image in-place — do not allocate another 2D matrix.

Example
Input:  [[1,2,3],[4,5,6],[7,8,9]]
Output: [[7,4,1],[8,5,2],[9,6,3]]

Input:  [[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 Core Intuition: Transpose + Reverse

A 90° clockwise rotation maps element at (i, j) to position (j, n-1-i). This can be decomposed into two simpler operations:

  1. Transpose: Swap matrix[i][j] with matrix[j][i] for all i < j. After this, element at (i,j) moves to (j,i).
  2. Reverse each row: Reverse every row in place. After this, element at (j,i) moves to (j, n-1-i).

💡 Rotation Cheat Sheet

Visual Proof

Step-by-step on [[1,2,3],[4,5,6],[7,8,9]]
Original:        After Transpose:   After Row Reverse:
1 2 3            1 4 7              7 4 1
4 5 6    →→→     2 5 8    →→→       8 5 2
7 8 9            3 6 9              9 6 3   ✅ 90° CW

Code in Python, Java & JavaScript

Python 3
def rotate(matrix: list[list[int]]) -> None:
    n = len(matrix)

    # Step 1: Transpose (swap across main diagonal)
    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()
Java
class Solution {
    public void rotate(int[][] matrix) {
        int n = matrix.length;

        // Step 1: Transpose
        for (int i = 0; i < n; i++)
            for (int j = i + 1; j < n; j++) {
                int temp = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i] = temp;
            }

        // Step 2: Reverse each row
        for (int[] row : matrix) {
            int left = 0, right = n - 1;
            while (left < right) {
                int temp = row[left];
                row[left++] = row[right];
                row[right--] = temp;
            }
        }
    }
}
JavaScript
var rotate = function(matrix) {
    const n = matrix.length;

    // Step 1: Transpose
    for (let i = 0; i < n; i++)
        for (let j = i + 1; j < n; j++)
            [matrix[i][j], matrix[j][i]] = [matrix[j][i], matrix[i][j]];

    // Step 2: Reverse each row
    for (const row of matrix)
        row.reverse();
};

Complexity Analysis

📊 Complexity

Rotation Variants

RotationSteps
90° ClockwiseTranspose → Reverse each row
90° Counter-ClockwiseReverse each row → Transpose
180°Reverse each row → Reverse each column

How to Actually Remember This

✅ Spaced Repetition Schedule

  1. Today: Implement LC #48. Draw the transpose step and reverse step on paper first.
  2. Day 3: Modify your solution for 90° counter-clockwise. Verify with a 3×3 example.
  3. Day 7: Solve LC #54 (Spiral Matrix) and LC #73 (Set Matrix Zeroes) to complete the matrix simulation set.

Tag on DSAPrep.dev under Matrix and Math.

Track on DSAPrep.dev →