Resource · Guide or article
Square Matrix In-Place Rotation
Rotates an n by n matrix 90 degrees clockwise without allocating a second matrix, by doing two reflections in a row.
Reviewed
Open the original: Square Matrix In-Place Rotation (opens the original site in a new tab)Add to a roadmap
Keep this resource with a roadmap you follow. It appears as your own extra resource; the roadmap's steps don't change and it doesn't count toward your tracked steps.
Source content: English
You are given an n x n 2D matrix (representing an image). Rotate the matrix by 90 degrees (clockwise). You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation. Rotate the input matrix in-place such that it becomes:
PrepKit overview
PrepKit editors wrote and reviewed this overview from the source repository. It is not text from the resource itself.
- Suited to
- The source does not state a level
- Format
- Lesson
What it covers
- 3x3 and 4x4 examples with before and after matrices
- Two reflection orders that both produce the rotation
- A paper-square trick for working out which reflections to use
- Text diagram tracing a top row through a diagonal and then a horizontal reflection
How to use it
PrepKit study advice, not a claim made by the source.
Rotate the 4x4 example on paper using one reflection order, code it, and confirm the output matches. Then explain how you would rotate counterclockwise.
Scope and limitations
No complexity figures or code on the page, and part of the algorithm section is garbled ('Or we also could Furthermore'). The only reference is LeetCode.
Access and format
README on GitHub, trekhleb/javascript-algorithms (MIT). English, free to read.
Reviewed by PrepKit on Oct 2, 2026. Listing source commit: 85293e3e2b88
Evidence behind this overview
- src/algorithms/uncategorized/square-matrix-rotation/README.md
Pages that include this
Get the PrepKit newsletter
Optional. You don't need it to use PrepKit.