array rotation
2D array
in-place algorithm
rectangular matrix
coding interview questions

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.

Practice algorithms

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.

text
1 2 3
4 5 6

rotates to:

text
4 1
5 2
6 3

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.

python
1def rotate_square_clockwise(matrix):
2    n = len(matrix)
3
4    for row in range(n):
5        for col in range(row + 1, n):
6            matrix[row][col], matrix[col][row] = matrix[col][row], matrix[row][col]
7
8    for row in matrix:
9        row.reverse()
10
11
12matrix = [
13    [1, 2, 3],
14    [4, 5, 6],
15    [7, 8, 9],
16]
17
18rotate_square_clockwise(matrix)
19print(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.

python
1def rotate_rect_clockwise(matrix):
2    rows = len(matrix)
3    cols = len(matrix[0])
4    return [
5        [matrix[rows - 1 - r][c] for r in range(rows)]
6        for c in range(cols)
7    ]
8
9
10matrix = [
11    [1, 2, 3],
12    [4, 5, 6],
13]
14
15rotated = rotate_rect_clockwise(matrix)
16print(rotated)

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.

python
1def rotate_180_in_place(matrix):
2    rows = len(matrix)
3    cols = len(matrix[0])
4
5    for r in range(rows):
6        for c in range(cols):
7            rr = rows - 1 - r
8            cc = cols - 1 - c
9            if (r, c) >= (rr, cc):
10                continue
11            matrix[r][c], matrix[rr][cc] = matrix[rr][cc], matrix[r][c]
12
13
14matrix = [
15    [1, 2, 3],
16    [4, 5, 6],
17]
18rotate_180_in_place(matrix)
19print(matrix)

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 cols input becomes cols 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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.