mediumMath & GeometryMicrosoftGoogle
Spiral Reader
A field surveyor walks a rectangular garden in a spiral from the top-left and records what they pass.
Given an m×n matrix, return all elements in spiral order.
Choose approach
1
2
3
4
5
6
7
8
9
1/2
Narrationstep 1 of 2
Peeled ring; order so far: [1,2,3,6,9,8,7,4].
Variables
top=1
bot=1
left=1
right=1
How to think about it
- Guard the two middle passes when a single row or column remains.
- 4 passes per ring.
Edge cases to catch in interview
- Single row -> left to right.
- Single column -> top to bottom.
Examples
Example 1
[1,2,3,4,8,12,11,10,9,5,6,7]{"grid":[[1,2,3,4],[5,6,7,8],[9,10,11,12]]}
Try your own input
SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed