Pattern Recognition Drill
Medium Math & Geometry
The Problem
Given an m×n matrix, return all elements in spiral order.
What approach would you use?
Think about it before scrolling down.
Track 4 boundaries. Traverse right, down, left, up; shrink after each. O(m*n).
Mark visited cells, turn right when hitting a wall or visited cell. Same O(m*n).
Common Trap
Check boundary conditions after EACH direction — the inner boundaries may have already crossed.