Rotate 2D rectangular array in-place
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
A 90-degree rotation of a square matrix can be done in place because the output has the same shape as the input. A true rectangular matrix is different: a rows x cols matrix becomes cols x rows after rotation, so the shape changes, and that makes the usual in-place algorithm impossible for the standard list-of-lists representation.
Why Square And Rectangular Cases Differ
Consider a 2 x 3 matrix. After a clockwise rotation, it becomes 3 x 2.
rotates to:
The output no longer fits into the same row structure. That is why the classic in-place transpose-plus-reverse trick only applies directly to square matrices.
Square Matrices Can Be Rotated In Place
For comparison, here is the standard in-place clockwise rotation for a square matrix.
That works because the matrix remains 3 x 3 after rotation.
Rectangular Rotation Usually Requires New Storage
For a rectangular matrix stored as a list of rows, the practical solution is to build a new matrix.
This returns a new 3 x 2 matrix, which is the correct shape.
What "In Place" Could Mean In A Different Representation
If the matrix is stored in one flat array rather than nested row lists, there are advanced permutation-based techniques that can rearrange the values with limited extra memory. But that is a different problem from rotating a conventional 2D array in place.
In most interview and application contexts, "2D rectangular array in place" really has a short answer: not in the usual representation for a 90-degree rotation, because the dimensions change.
Other Rotations
A 180-degree rotation is special because the shape does not change. That can be done in place even for rectangular matrices.
That works because the rotated matrix still has the same number of rows and columns.
Communicating The Constraint Clearly
If this comes up in an interview, say the constraint out loud: a 90-degree rotation of a non-square matrix changes the dimensions, so either the representation must change or a new output structure is required. That answer is stronger than trying to force the square-matrix algorithm onto the wrong case.
Common Pitfalls
The most common mistake is applying the square transpose-and-reverse algorithm to a rectangular matrix. Another is saying an algorithm is in place even though it silently allocates a new matrix of rotated dimensions. Developers also sometimes miss that 180-degree rotation is a different case and actually can be done in place for rectangles. Finally, if the matrix rows are ragged rather than truly rectangular, even the allocate-a-new-result approach needs extra validation.
Summary
- A 90-degree in-place rotation works naturally for square matrices, not standard rectangular ones.
- Rectangular
rows x colsinput becomescols x rows, so the shape changes. - In a normal 2D row-based representation, the practical solution is to allocate a new matrix.
- A 180-degree rotation is different and can be done in place for rectangles.
- Be explicit about representation constraints instead of forcing the wrong algorithm.
Related reading

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.