How to Rotate a Matrix by 90, 180, or 270 Degrees

To rotate a square matrix 90 degrees clockwise, you transpose it (swap rows and columns), then reverse each row. This two-step approach works in place, meaning you don’t need a second matrix to hold the result. The time complexity is O(n²) regardless of method, but the in-place version uses O(1) extra space compared to O(n²) when copying into a new matrix.

The Two-Step Approach: Transpose, Then Reverse

A clockwise 90-degree rotation breaks down into two operations you can perform directly on the original matrix:

  • Step 1: Transpose. Swap every element at position [i][j] with the element at [j][i]. This flips the matrix along its main diagonal, turning rows into columns.
  • Step 2: Reverse each row. Take every row in the transposed matrix and reverse the order of its elements.

Here’s a concrete example. Start with this 3×3 matrix:

1 2 3
4 5 6
7 8 9

After transposing (swapping rows and columns):

1 4 7
2 5 8
3 6 9

After reversing each row:

7 4 1
8 5 2
9 6 3

That’s your 90-degree clockwise rotation, done in place with no extra memory.

How the Transpose Works in Code

The transpose step uses two nested loops. The outer loop iterates through rows starting at index 0, and the inner loop starts at the column one past the current row (j = i + 1). Starting the inner loop at i + 1 rather than 0 is important: it prevents you from swapping elements twice, which would undo the transpose entirely.

For each pair of positions, you swap matrix[i][j] with matrix[j][i] using a temporary variable. In pseudocode:

for i = 0 to n-1:
  for j = i+1 to n-1:
    temp = matrix[i][j]
    matrix[i][j] = matrix[j][i]
    matrix[j][i] = temp

The reversal step is simpler. For each row, use two pointers starting at opposite ends and swap inward until they meet:

for each row in matrix:
  start = 0, end = row.length – 1
  while start < end:
    swap row[start] and row[end]
    start++, end–

Counter-Clockwise Rotation

For a 90-degree counter-clockwise rotation, you reverse the order of the two operations: reverse each row first, then transpose. Alternatively, you can transpose first and then reverse each column instead of each row. Both produce the same result.

Using the same starting matrix:

1 2 3
4 5 6
7 8 9

Reverse each row first:

3 2 1
6 5 4
9 8 7

Then transpose:

3 6 9
2 5 8
1 4 7

That’s a 90-degree counter-clockwise rotation.

180 and 270-Degree Rotations

A 180-degree rotation is equivalent to applying the 90-degree rotation twice. In practice, you can skip the transpose entirely and just reverse each row, then reverse each column (or equivalently, reverse the order of the rows and then reverse each row). The simplest way to think about it: every element at position [i][j] moves to [n-1-i][n-1-j].

A 270-degree clockwise rotation is identical to a 90-degree counter-clockwise rotation. So you can use the reverse-then-transpose method described above. If you already have a working 90-degree clockwise function, applying it three times also works, though it’s less efficient.

Using an Extra Matrix

If you don’t need to save memory, the simplest approach is to create a new matrix and copy elements directly into their rotated positions. For a clockwise 90-degree rotation of an n×n matrix, the element at [i][j] moves to [j][n-1-i] in the new matrix.

This is easier to reason about and less error-prone, but it uses O(n²) extra space since you’re allocating an entirely new grid. The time complexity is the same O(n²) either way, because you still need to touch every element once.

Built-In Functions in Python

If you’re working in Python with NumPy, the numpy.rot90 function handles rotation in one call:

numpy.rot90(matrix, k=1, axes=(0, 1))

The k parameter controls how many 90-degree counter-clockwise rotations to apply. Setting k=1 rotates 90 degrees counter-clockwise, k=2 gives 180 degrees, and k=3 gives 270 degrees counter-clockwise (equivalent to 90 degrees clockwise). The axes parameter defines which plane to rotate in, which matters for arrays with more than two dimensions.

Without NumPy, a common Python one-liner for clockwise 90-degree rotation is list(zip(*matrix[::-1])). This reverses the row order, then uses zip to regroup elements by column. It creates a new matrix rather than modifying in place.

Why Matrix Rotation Matters

The most tangible application is image processing. A digital image is just a matrix of pixel values, and rotating the image means rotating that matrix. Every time you rotate a photo on your phone, the underlying operation is a matrix rotation applied to the pixel grid.

The same math extends into 3D. Computer graphics, robotics, and game engines use rotation matrices to spin objects in three-dimensional space. A 3D rotation around, say, the z-axis uses a 4×4 transformation matrix with sine and cosine values that determine the rotation angle. These transformations are how game characters turn, how robotic arms move, and how camera perspectives shift in film.

In coding interviews, matrix rotation is a common problem because it tests your ability to manipulate indices and work within space constraints. The in-place solution using transpose and reverse is the standard expected answer, since it demonstrates both the algorithmic thinking and the O(1) space optimization.