intermediate9 min read·Updated October 14, 2026

Rotate Matrix 90° Clockwise In-Place: Tracing [[1,2,3],[4,5,6],[7,8,9]]

Master 90° clockwise matrix rotation in-place without extra memory. Follow a complete visual trace of a 3×3 grid, mental models, and complexity analysis.

By Learnisim AI·Published October 14, 2026
LEARNING ARCSetup → Model in the example → Full trace → Twist → Edge cases → Apply
PREREQUISITES
  • 2D array indexing
  • basic loops
  • matrix transposition
In-Place 90° Clockwise Matrix Rotation (Transpose + Reverse) 1. Original Matrix 1 2 3 4 5 6 7 8 9 Step A: Transpose 2. Transpose Matrix Swap A[i][j] with A[j][i] 1 4 7 2 5 8 3 6 9 Step B: Reverse Rows 3. Final Rotated (90°) [[7,4,1],[8,5,2],[9,6,3]] 7 4 1 8 5 2 9 6 3 Why In-Place Rotation? Zero Extra Memory Allocation Avoids duplicating 4K buffers or datasets. O(1) Auxiliary Space Complexity Swaps happen directly in original memory. Prevents Data Overwriting Structured 2-step geometry prevents loss. The Two-Step Geometry 1. Transpose across diagonal Swap element matrix[i][j] with [j][i]. 2. Reverse each row Mirror elements horizontally left-to-right. Combined Result Perfect 90° clockwise rotation achieved. Mathematical Notation Matrix Transposition A^T_{i,j} = A_{j,i} Row Reversal A_{i, j} <---> A_{i, n-1-j} Algorithm Time Complexity O(N^2) time visits every element.
Rotate [[1,2,3],[4,5,6],[7,8,9]] overview diagram
Why

Why Rotate a Matrix In-Place Without Extra Memory

Phase 1: The Memory Cost of Naive Rotation

Imagine you are handed a grid of pixels or data values—specifically, our 33 matrix
begin{bmatrix} 1 & 2 & 3 \ 4 & 5 & 6 \ 7 & 8 & 9 end{bmatrix}
—and your system demands it rotated 90° clockwise so that it becomes
begin{bmatrix} 7 & 4 & 1 \ 8 & 5 & 2 \ 9 & 6 & 3 end{bmatrix}
.
The most intuitive first instinct is to allocate a brand-new 33 matrix, loop through the old rows and columns, and copy elements into their new rotated homes. For a tiny 33 grid, that extra allocation costs almost nothing. But scale that up to a 4K image buffer or a massive dataset where , and allocating an entirely duplicate matrix doubles your memory footprint instantly.
In memory-constrained environments, graphics pipelines, or embedded systems, that extra allocation is a luxury you cannot afford. You must perform the rotation in-place—meaning you rearrange the existing elements inside the original memory space without utilizing a second matrix.
Attempting this by blindly swapping values immediately leads to a classic failure mode: overwriting data you still need before you have moved it. Without a structured geometric plan, you end up with a mangled matrix of duplicated numbers and lost values.
Matrix 90° Clockwise Rotation In-Place (O(1) Extra Space) Step 1: Transpose Matrix (Swap [i][j] with [j][i]) ➔ Step 2: Reverse Each Row 1. Original 3×3 Matrix 1 2 3 4 5 6 7 8 9 Transpose 2. Transposed (Swap i,j) 1 4 7 2 5 8 3 6 9 Reverse Rows 3. Final Rotated 90° 7 4 1 8 5 2 9 6 3 Why In-Place Rotation? • Avoids allocating a duplicate O(N²) matrix • Critical for 4K image buffers & embedded sys • Prevents overwriting data before it's moved • Achieves exact O(1) auxiliary memory Result: Maximum cache locality & speed Phase 1: Matrix Transpose • Swap element matrix[i][j] with matrix[j][i] • Only swap where j > i to avoid double-swaps • Example: Swap (1,2) with (2,1) — values 2 & 4 • Reflects all matrix elements across main diagonal Time Complexity: O(N²) Phase 2: Row Reversal • Iterate through each row of the transposed grid • Swap left-most elements with right-most elements • Example row: [1, 4, 7] becomes [7, 4, 1] • Completes the exact 90° clockwise transformation Combined Space: O(1) auxiliary
Why Rotate a Matrix In-Place Without Extra Memory diagram
Model

The Two-Step Geometry of In-Place Matrix Rotation

Phase 2: Visualizing the Transformation

When we look at our locked working example matrix
, rotating it 90 degrees clockwise means the top row [1, 2, 3] must become the rightmost column, and the bottom row [7, 8, 9] must become the leftmost column. Doing this without allocating a brand-new grid feels tricky because direct element-shifting causes overwriting collisions. Instead, we can split the rotation into two simpler geometric reflections: transposing the matrix across its main diagonal, and then reversing each individual row.
To see how this works on our locked example, the transpose operation swaps every element at coordinates (i, j) with (j, i). The main diagonal elements (1, 5, 9) stay firmly in place, while pairs like 2 and 4, 3 and 7, and 6 and 8 swap positions. Once transposed, the grid becomes
. At this stage, the columns are correct, but they are arranged from left to right instead of right to left. Reversing each row of this transposed intermediate state instantly snaps the elements into the final expected result of
.
Try this: Given the 2x2 matrix [[1, 2], [3, 4]], apply the transpose operation first, then predict what the matrix looks like before the row-reversal step.
The Two-Step Geometry of In-Place Matrix Rotation (90° Clockwise) Original Matrix → Step 1: Transpose (Swap across diagonal) → Step 2: Reverse Each Row 1. Original Matrix 1 2 3 4 5 6 7 8 9 Top row [1,2,3] needs to become right column. Direct Overwriting Fails Shifting elements directly causes collision & data loss. Step 1 2. Transpose Matrix 1 4 7 2 5 8 3 6 9 Swap every (i, j) with (j, i) Diagonal (1, 5, 9) stays fixed. Diagonal Reflection Pairs like (2,4) and (3,7) swap places cleanly. Step 2 3. Reverse Each Row 7 4 1 8 5 2 9 6 3 Flip each row horizontally. Columns snap into place! Final 90° Rotation O(1) Extra Space achieved without allocating new matrix.
The Two-Step Geometry of In-Place Matrix Rotation diagram
Notation

Mathematical Notation for In-Place Matrix Rotation

Phase 3: Notation & definitions

To rigorously describe our 90° clockwise in-place rotation for the locked example
, we must define coordinate transformations for every element in an grid. Let be an matrix where denotes the element at row and column , with indices . For our 33 working example, .
The overall 90° clockwise rotation operator maps an element's initial position to its final position :
We achieve this via two sequential matrix operations. First, the transpose operation swaps elements across the main diagonal ():
Applying to our working example transforms into the intermediate transposed state:
Second, the horizontal reversal operator reflects each row around its vertical center:
Applying to completes the rotation, yielding our expected result:
Try this: Using the coordinate mapping formula R_90(A_{i,j}) = A_{j, n - 1 - i}, compute the final destination coordinate for the element currently at row i = 0, column j = 2 in our 3x3 matrix [[1,2,3],[4,5,6],[7,8,9]].
Worked example

Tracing the Transpose and Reverse Steps on a 3 by 3 Matrix

Phase 4: Worked Example

Let us trace our locked example from start to finish. We are given a matrix and need to rotate it clockwise in-place.
Step 1: Transpose the matrix.
We iterate through the upper triangle where and swap with .
- Swap (2) and (4)
- Swap (3) and (7)
- Swap (6) and (8)
After transposing, the intermediate matrix looks like this:
Step 2: Reverse each row.
Next, we iterate through each row and reverse its elements in-place (swap index 0 with index 2).
- Row 0: reverse to
- Row 1: reverse to
- Row 2: reverse to
Result:
Try this: Given the 2x2 matrix [[1, 2], [3, 4]], write down the intermediate matrix after completing Step 1 (the transpose operation).
Phase 4: Worked Example — 3×3 Matrix Rotation Step 1: Transpose (Upper Triangle Swaps) → Step 2: Reverse Each Row 1. Initial Matrix 1 2 3 4 5 6 7 8 9 Original 3×3 state Diagonal stays fixed Transpose 2. After Transpose 1 4 7 2 5 8 3 6 9 Swap upper triangle e.g. (0,1) ⇄ (1,0) Reverse Rows 3. Final Result 7 4 1 8 5 2 9 6 3 Horizontal row reversal 90° Clockwise Rotation! Check: For 2×2 matrix [[1,2],[3,4]], Step 1 (Transpose) yields [[1,3],[2,4]]
Tracing the Transpose and Reverse Steps on a 3 by 3 Matrix diagram
Practice

Practice In-Place Rotation on a 2 by 2 Matrix

Now that you have seen how transpose and row-reversal transform our 3 by 3 matrix step by step, it is time to test your mental model on a smaller grid. Let us apply the exact same two-step procedure to our alternate working example: a 2 by 2 matrix.

Phase 5: Practice

Given the 2 by 2 matrix:
Perform the two-step in-place rotation—first transpose the matrix by swapping elements across the main diagonal, and then reverse each row. What is the resulting 2 by 2 matrix after these two operations?
Apply

Applying In-Place Rotation Patterns to Neighboring Geometric Transforms

Phase 6: Transfer

Now that you have mastered rotating the matrix [[1,2,3],[4,5,6],[7,8,9]] into [[7,4,1],[8,5,2],[9,6,3]] via transpose and row-reversal, you possess a structural pattern that applies to other geometric transformations. Instead of memorizing separate coordinate formulas for every angle, you can compose elementary operations—just like functions in algebra. For instance, what if you need to rotate counterclockwise by instead of clockwise?
Recall that a clockwise rotation is transpose followed by horizontal row reversal. By simply reversing the order of these two steps—or reversing rows first, then transposing—you achieve a counterclockwise rotation. Let us apply this derived technique to our working matrix.
First, reverse each row of our original matrix:
Next, transpose the resulting matrix by swapping elements across the main diagonal ():
This exact output is the counterclockwise rotation of [[1,2,3],[4,5,6],[7,8,9]]. By recognizing that complex matrix transformations decompose into basic index swaps, you can adapt the in-place pattern to rotations, vertical flips, or diagonal reflections with zero extra memory overhead.

FAQ

What is the result of rotating the 3×3 matrix [[1,2,3],[4,5,6],[7,8,9]] 90° clockwise in-place?
The resulting rotated matrix is [[7,4,1],[8,5,2],[9,6,3]], achieved by first transposing the matrix and then reversing each row.
Why is the in-place approach preferred over using an auxiliary matrix?
Using an auxiliary matrix requires O(N²) extra space. The in-place approach achieves the rotation using O(1) auxiliary space by mutating the original matrix directly.
What are the two geometric steps required for an in-place 90° clockwise rotation?
First, transpose the matrix by swapping elements across the main diagonal (matrix[i][j] with matrix[j][i]). Second, reverse every row in the matrix.
What is the time and space complexity of this algorithm?
The time complexity is O(N²) because every element in the N×N matrix is visited during the transpose and reverse steps. The space complexity is O(1) as no extra data structures are allocated.

Keep learning