LeetCode Problem

How to Solve Rotate Image

To solve Rotate Image, start by transposing the matrix, swapping rows and columns to align elements along the diagonal. Then reverse each row to achieve a 90-degree clockwise rotation. This approach avoids extra space, directly modifies the input, and leverages array plus math patterns for predictable in-place transformations.

GhostInterview Help

Need help with Rotate Image without spending extra time grinding it?

GhostInterview can read Rotate Image from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #48Array plus MathReviewed 2026-03-08
Difficulty
Medium
Primary pattern
Array plus Math
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

To solve Rotate Image, start by transposing the matrix, swapping rows and columns to align elements along the diagonal. Then reverse each row to achieve a 90-degree clockwise rotation. This approach avoids extra space, directly modifies the input, and leverages array plus math patterns for predictable in-place transformations.

Problem Statement

You are given an n x n 2D matrix representing an image, and your task is to rotate the image 90 degrees clockwise. You must perform this rotation in-place without allocating another 2D matrix, modifying the original matrix directly.

For example, given matrix = [[1,2,3],[4,5,6],[7,8,9]], after rotation the matrix becomes [[7,4,1],[8,5,2],[9,6,3]]. 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]]

Example details omitted.

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

Example details omitted.

Constraints

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

Solution Approach

Transpose the Matrix

Swap elements across the diagonal: for each element at (i,j) where i < j, swap with element at (j,i). This aligns rows with columns using the array plus math pattern.

Reverse Each Row

After transposition, reverse every row in the matrix. This shifts elements into correct 90-degree rotated positions, completing the clockwise rotation in-place without extra memory.

Iterative Layered Rotation (Alternative)

Rotate the matrix layer by layer from outermost to innermost, moving four elements at a time. This method uses direct index calculations, highlighting the array plus math pattern and avoiding full transposition.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

Time complexity is O(n^2) because each element is visited during transposition and row reversal. Space complexity is O(1) since rotation is done in-place without extra matrix allocation.

What Interviewers Usually Probe

  • Focus on in-place array manipulation rather than creating a new matrix.
  • Check if candidate uses both transpose and row reversal, showing understanding of index math.
  • Ask for an explanation of how indices map before and after rotation to test array plus math reasoning.

Common Pitfalls or Variants

Common pitfalls

  • Trying to rotate using an extra matrix, which violates in-place constraint.
  • Reversing columns instead of rows after transposition, leading to counter-clockwise rotation.
  • Incorrectly swapping elements during transposition, causing overwrites and wrong output.

Follow-up variants

  • Rotate matrix 180 degrees clockwise in-place.
  • Rotate non-square matrix with minimal extra space using similar index math.
  • Rotate matrix counter-clockwise using transpose and column reversal pattern.

How GhostInterview Helps

  • Provides step-by-step in-place rotation logic for array plus math pattern recognition.
  • Highlights exact index mapping errors and common off-by-one mistakes during transposition.
  • Simulates interview feedback to ensure correct clockwise rotation without extra memory.

Topic Pages

FAQ

What is the most efficient method to solve Rotate Image?

Transpose the matrix and then reverse each row. This uses O(1) space and O(n^2) time for in-place rotation.

Can I use extra space to rotate the matrix?

No, the problem requires in-place rotation, so allocating another matrix violates constraints.

How do array plus math concepts apply to Rotate Image?

They guide how to calculate new positions for each element using index transformations without extra storage.

What if the matrix is not square?

Standard Rotate Image assumes n x n matrices; non-square matrices require adjusted index mapping or different rotation logic.

How do I avoid common pitfalls in Rotate Image?

Always swap elements correctly during transposition and reverse rows properly; double-check index calculations to prevent overwrites.

GhostInterview Solver

Need direct help with Rotate Image instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Rotate Image from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.