How to rotate a N x N matrix by 90 degrees?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
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.
Related reading
- How to run function on the deepest level only in a nested list?
- how to save shortest path in dijkstra algorithm
- How to select points at a regular density
- How to select strategy to reduce overfitting?
- How to set patience as default git diff algorithm
- How to solve my difficulty making algorithms?
- How to solve the following graph game
- How to solve Tic Tac Toe 4x4 game using Minimax Algorithm and Alpha Beta Pruning

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.