Spiral Order
Step 1 of 9
Four walls — top, bottom, left, right — hold the part of the board still unread.
The Idea
Do not chase a direction. Hold four walls — top, bottom, left, right — around the part still unread.
Walk the top wall left to right and push it down. Walk the right wall down and pull it in. Bottom back, left up, and repeat. The same four moves handle any rectangle; when the walls cross, you are done.
Real-World Example
Mowing a rectangular lawn edge-first: cut the outer strip, then the next one in, until you reach the middle. Each lap moves a wall inward and never crosses cut grass. The pattern shows up in interviews because it rewards a clean invariant instead of clever index arithmetic.
The Code
Your turn
What does this print?
print(spiral([[1, 2], [3, 4]]))Mini quiz
1 / 3