Ctrl K

Outward Spiral Walk

Medium
A field has rows rows and cols columns; row 0 is at the top and column 0 is on the left. A robot stands in cell (start_row, start_col) facing east (towards larger column numbers). It walks along an unwinding clockwise spiral: 1 step east, 1 south, 2 west, 2 north, 3 east, 3 south, 4 west, and so on. When the spiral takes it off the field, the robot keeps walking outside; it will come back later. Return the [row, column] coordinates of every cell of the field in the order the robot first reaches them, starting with the starting cell. The walk ends as soon as all rows · cols cells have been visited.
Example 1
rows=2, cols=3, start_row=0, start_col=1 → [[0, 1], [0, 2], [1, 2], [1, 1], [1, 0], [0, 0]]

Start at (0, 1): one step east to (0, 2), south to (1, 2), two steps west to (1, 1) and (1, 0), then north to (0, 0). All 6 cells are collected at that point.

Example 2
rows=3, cols=1, start_row=2, start_col=0 → [[2, 0], [1, 0], [0, 0]]

All steps of the first lap (1 east, 1 south, 2 west, 2 north) stay outside the field. The robot meets cell (1, 0) during the 3 steps east, and reaches (0, 0) even later, during the 5 steps east.

Example 3
rows=1, cols=1, start_row=0, start_col=0 → [[0, 0]]
Constraints
  • 1 ≤ rows, cols ≤ 100
  • 0 ≤ start_row < rows
  • 0 ≤ start_col < cols
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSpiral Readout MediumSpiral-Numbered Square MediumNext Step of a Cell Colony MediumZigzag Diagonal Sweep MediumMatch-Three Cascade Medium
loading Python…
Input
rows=2, cols=3, start_row=0, start_col=1
Expected
[[0, 1], [0, 2], [1, 2], [1, 1], [1, 0], [0, 0]]

Run checks the first 3 tests in your browser. Submit checks all 77 tests on the server, hidden ones included.