Rotate Image
Rotate an n×n matrix 90 degrees clockwise in place.
Why does this pattern fit?
Restate the exact job
Rotate an n×n matrix 90 degrees clockwise in place.
Clockwise rotation equals transpose followed by reversing each row.
O(n²) time · O(1) space
Transposing without row reversal reflects rather than rotates.
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.