Spiral Matrix
Return matrix values in clockwise spiral order.
Why does this pattern fit?
Restate the exact job
Return matrix values in clockwise spiral order.
Four boundaries shrink after their corresponding edge is consumed.
O(mn) time · O(1) extra
Skipping boundary guards duplicates the final row or column.
How to solve Spiral Matrix
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
Return matrix values in clockwise spiral order.
Why Matrix traversal & transforms fits
Four boundaries shrink after their corresponding edge is consumed.
State to maintain
Top, bottom, left, and right bounds.
Transition
Traverse top/right, then bottom/left only if bounds still cross.
Time and space
O(mn) time · O(1) extra
Counterexample to the tempting mistake
Skipping boundary guards duplicates the final row or column.
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.