100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

How Do You Rotate a Matrix 90 Degrees In Place?

Learn the transpose-then-reverse technique to rotate an n x n matrix 90 degrees clockwise in place with O(1) extra space.

mediumQ109 of 227 in Data Structures & Algorithms Est. time: 5 minsLast updated:
Open Code Lab

Expected Interview Answer

To rotate an n x n matrix 90 degrees clockwise in place, first transpose the matrix by swapping element (i, j) with (j, i), then reverse each row, achieving O(n²) time and O(1) extra space.

Transposing flips the matrix across its main diagonal, turning rows into columns. Reversing each row after the transpose then produces the clockwise rotation; reversing each column instead of transposing-then-row-reversing gives counter-clockwise rotation with a different combination of steps. A naive approach copies into a brand-new n x n matrix using the mapping new[j][n-1-i] = old[i][j], which is also O(n²) time but costs O(n²) extra space. The in-place transpose-and-reverse trick is preferred whenever the problem explicitly forbids allocating a second matrix.

  • O(n²) time, O(1) extra space, truly in place
  • Two simple, well-understood primitive operations (transpose, reverse)
  • Easily adapted for counter-clockwise rotation by reversing the order of steps
  • No risk of off-by-one errors from manual index remapping

AI Mentor Explanation

A field-placement grid can be spun 90 degrees by first transposing it — swapping each fielder’s row and column position across the diagonal — and then reversing every row of the grid. The transpose alone just mirrors positions diagonally, not a true rotation, but reversing each row afterward completes the clockwise spin exactly. A captain who instead tries to directly compute each fielder’s new spot by hand risks mixing up rows and columns under match pressure. Splitting the spin into two simple, well-drilled steps avoids that entirely, using the same grid with no substitute sheet.

Step-by-Step Explanation

  1. Step 1

    Transpose the matrix

    For every i < j, swap matrix[i][j] with matrix[j][i], mirroring across the main diagonal.

  2. Step 2

    Reverse each row

    Reverse the elements within every row in place to complete a 90-degree clockwise rotation.

  3. Step 3

    Verify with a small example

    Trace a 3x3 matrix by hand to confirm the transpose-then-reverse sequence produces the expected clockwise result.

  4. Step 4

    Adapt for counter-clockwise

    For counter-clockwise rotation, transpose then reverse each column instead of each row (or reverse rows first, then transpose).

What Interviewer Expects

  • Correctly describe transpose as swapping matrix[i][j] with matrix[j][i]
  • Explain why transpose alone is not a rotation, and row reversal completes it
  • State O(n²) time and O(1) extra space for the in-place approach
  • Know how to adapt the technique for counter-clockwise rotation

Common Mistakes

  • Transposing the full matrix twice (i and j both looping 0..n) instead of only i < j, undoing the swap
  • Reversing columns instead of rows for clockwise rotation
  • Assuming the matrix must be square; the in-place trick only works for n x n matrices
  • Allocating a new matrix when the problem explicitly requires in-place rotation

Best Answer (HR Friendly)

To rotate a square matrix 90 degrees without extra memory, I first transpose it, flipping it across the diagonal, and then reverse each row. Those two simple steps combined give the correct clockwise rotation, and they are much easier to get right than manually recomputing every cell’s new position.

Code Example

In-place 90-degree clockwise matrix rotation
def rotate_matrix(matrix):
    n = len(matrix)

    # Transpose: swap matrix[i][j] with matrix[j][i]
    for i in range(n):
        for j in range(i + 1, n):
            matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]

    # Reverse each row
    for row in matrix:
        row.reverse()

    return matrix

m = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(rotate_matrix(m))  # [[7, 4, 1], [8, 5, 2], [9, 6, 3]]

Follow-up Questions

  • How would you rotate the matrix 90 degrees counter-clockwise instead?
  • How would you rotate a non-square (m x n) matrix?
  • How would you rotate the matrix 180 degrees?
  • Can you rotate the matrix by traversing it layer by layer instead of transpose-and-reverse?

MCQ Practice

1. What two operations, in order, produce an in-place 90-degree clockwise matrix rotation?

Transposing mirrors the matrix across the diagonal, and reversing each row afterward completes the clockwise rotation.

2. What is the extra space complexity of the transpose-and-reverse in-place rotation?

Both the transpose and the row reversal operate on the original matrix with only constant extra variables, giving O(1) space.

3. Why does transposing a matrix alone NOT produce a 90-degree rotation?

Transpose swaps rows and columns across the diagonal, which is a reflection, not a rotation; reversing rows afterward supplies the missing rotation.

Flash Cards

What are the two steps of in-place 90-degree clockwise matrix rotation?Transpose the matrix, then reverse each row.

What does transposing a matrix mean?Swapping matrix[i][j] with matrix[j][i] for every pair, mirroring across the main diagonal.

What is the time complexity of in-place matrix rotation?O(n²), since every cell is visited a constant number of times.

How do you adapt the technique for counter-clockwise rotation?Transpose the matrix, then reverse each column instead of each row.

1 / 4

Continue Learning