PROBLEM 68 OF 75

Rotate Image

Rotate an n×n matrix 90 degrees clockwise in place.

PATTERNMatrix traversal & transformsWrite the coordinate mapping before the loops.
SPOT THE SIGNAL

Why does this pattern fit?

FRAME · 1 OF 4

Restate the exact job

Rotate an n×n matrix 90 degrees clockwise in place.

WHY THIS FITS

Clockwise rotation equals transpose followed by reversing each row.

COMPLEXITY

O(n²) time · O(1) space

COMMON FAILURE

Transposing without row reversal reflects rather than rotates.

THE REASONING, IN ONE PLACE

How to solve Rotate Image

The goal is to solve this problem from the pattern, not to memorize a finished answer. Use this as a check after your own attempt.

What the question asks

Rotate an n×n matrix 90 degrees clockwise in place.

Why Matrix traversal & transforms fits

Clockwise rotation equals transpose followed by reversing each row.

State to maintain

Matrix coordinates above the diagonal.

Transition

Swap matrix[r][c] with matrix[c][r], then reverse every row.

Time and space

O(n²) time · O(1) space

Counterexample to the tempting mistake

Transposing without row reversal reflects rather than rotates.

Prove it again tomorrow

Close this page. Rebuild the state and transition from memory, write a test that exposes the mistake above, then solve a fresh input without looking back. A same-day reread is practice, not proof of retention.

Open blank recall ↗