Spiral Matrix III - Problem

You start at cell (rStart, cStart) of an rows × cols grid facing east. The northwest corner is at the first row and column in the grid, and the southeast corner is at the last row and column.

You will walk in a clockwise spiral shape to visit every position in this grid. Whenever you move outside the grid's boundary, we continue our walk outside the grid (but may return to the grid boundary later).

Eventually, we reach all rows × cols spaces of the grid. Return an array of coordinates representing the positions of the grid in the order you visited them.

Input & Output

Example 1 — Small 2×2 Grid
$ Input: rows = 1, cols = 4, rStart = 0, cStart = 0
Output: [[0,0],[0,1],[0,2],[0,3]]
💡 Note: Start at (0,0), walk east: (0,0)→(0,1)→(0,2)→(0,3). All positions are in-bounds and visited in spiral order.
Example 2 — Square Grid Starting Center
$ Input: rows = 5, cols = 6, rStart = 1, cStart = 4
Output: [[1,4],[1,5],[2,5],[2,4],[2,3],[1,3],[0,3],[0,4],[0,5],[3,5],[3,4],[3,3],[3,2],[2,2],[1,2],[0,2],[4,5],[4,4],[4,3],[4,2],[4,1],[3,1],[2,1],[1,1],[0,1],[4,0],[3,0],[2,0],[1,0],[0,0]]
💡 Note: Start at (1,4), spiral outward: 1 east, 1 south, 2 west, 2 north, 3 east, etc. Only record positions within the 5×6 grid bounds.
Example 3 — Single Cell Grid
$ Input: rows = 1, cols = 1, rStart = 0, cStart = 0
Output: [[0,0]]
💡 Note: Edge case: only one cell exists, so return that single position.

Constraints

  • 1 ≤ rows, cols ≤ 100
  • 0 ≤ rStart < rows
  • 0 ≤ cStart < cols

Visualization

Tap to expand
Spiral Matrix III - Simulation Approach INPUT 1 x 4 Grid (rows=1, cols=4) START (0,1) (0,2) (0,3) Facing: EAST --> Input Parameters rows = 1 cols = 4 rStart = 0 cStart = 0 Total cells: 4 Start at (0,0) ALGORITHM STEPS 1 Initialize Start at (rStart, cStart) Add (0,0) to result 2 Spiral Pattern Move: E,S,W,W,N,N,E,E,E... Steps increase every 2 turns 3 Check Bounds If inside grid: add to result If outside: continue spiral 4 Repeat Until all cells visited Count = rows * cols Direction Order (Clockwise) EAST SOUTH WEST NORTH Steps: 1,1,2,2,3,3,4,4... FINAL RESULT Visit Order Visualization 1st (0,0) 2nd (0,1) 3rd (0,2) 4th (0,3) Output Array [[0,0],[0,1],[0,2],[0,3]] 4 positions visited - OK Execution Trace Move EAST 1: (0,0)-->(0,1) [OK] Move EAST 1: (0,1)-->(0,2) [OK] Move EAST 1: (0,2)-->(0,3) [OK] Key Insight: The spiral pattern increases step count every 2 direction changes: 1,1,2,2,3,3,4,4... We continue the spiral even outside the grid, only adding coordinates when inside bounds. For this 1x4 grid starting at (0,0) facing east, we simply move east to visit all cells in order. TutorialsPoint - Spiral Matrix III | Simulation - Follow Spiral Pattern
Asked in
Google 15 Amazon 12 Microsoft 8 Facebook 6
35.4K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen