How to rotate a N x N matrix by 90 degrees?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Rotating an N x N matrix by 90 degrees is a standard array-manipulation problem. The two most useful solutions are a simple copy-based approach that is easy to reason about and an in-place approach that uses transpose-plus-reverse to achieve O(1) extra space.
Understand the Coordinate Mapping
For a clockwise rotation, an element at row r and column c moves to:
That mapping is the real problem. Every correct implementation, whether it uses extra space or not, follows this movement rule.
Take this matrix:
After a clockwise rotation, the result should be:
Keeping that target in mind makes it easier to verify the algorithm.
Copy-Based Solution
The clearest approach is to build a new matrix and write each value into its rotated position.
This solution is excellent when clarity matters more than memory. It is also the easiest version to adapt to non-square matrices, because it avoids in-place overwriting issues.
In-Place Solution: Transpose Then Reverse
For an N x N matrix, the classic in-place solution has two phases:
- transpose across the main diagonal,
- reverse each row.
Why this works:
- transposing turns rows into columns,
- reversing each row then reorders those columns into clockwise orientation.
This is the version most interviewers expect when they specifically ask for an in-place solution.
Counterclockwise Rotation
If the rotation should go 90 degrees counterclockwise, you can still transpose first, but the second step changes. Instead of reversing each row, reverse the order of the rows in the matrix.
The direction matters. Many bugs in this problem are simply clockwise and counterclockwise logic being mixed up.
Complexity and Constraints
Both common solutions touch each element a constant number of times, so time complexity is O(N^2). The difference is in extra memory:
- copy-based rotation uses
O(N^2)extra space, - in-place transpose-and-reverse uses
O(1)extra space.
The in-place method assumes the matrix is square. If the matrix is rectangular, the shape changes after a 90-degree rotation, so you typically need a new output matrix.
Common Pitfalls
- Using the wrong coordinate mapping and producing a counterclockwise result by mistake.
- Swapping every transposed pair twice instead of starting the inner loop at
r + 1. - Applying the in-place method to a non-square matrix.
- Writing rotated values back into the original matrix during the copy-based approach and corrupting unread data.
- Forgetting to clarify clockwise versus counterclockwise rotation before coding.
Summary
- A clockwise 90-degree rotation maps
(r, c)to(c, n - 1 - r). - The simplest solution builds a new matrix using that mapping.
- The standard in-place solution is transpose first, then reverse each row.
- Counterclockwise rotation uses a different second step after transpose.
- The optimized in-place version works for square matrices and uses
O(1)extra space.

