Read a Grid in a Spiral
Talk it through out loud with your AI coach.
The problem
- medium
- Pro
Given a grid of numbers with m rows and n columns, return all its values in spiral order: along the top row left to right, down the right side, back along the bottom row, up the left side, then inwards. For the three by three grid of one to nine in reading order, the answer is one, two, three, six, nine, eight, seven, four, five.
What a strong answer covers
- A way to track the unvisited part of the grid as it shrinks.
- The order of the four walks and how each one updates the bounds.
- The extra care a leftover single row or column needs.
- The cost, and the grid shapes worth testing.
Common mistake: Testing only square grids, so a leftover middle row or column gets read twice on rectangular ones.
Knowing this is half of it. Saying it clearly out loud is the part the coach helps you practise.
Practise it out loud