List Grid Cells Encountered by an Expanding Clockwise Spiral

Quick Overview

List Grid Cells Encountered by an Expanding Clockwise Spiral. Start at (rStart,cStart) in a zero-indexed rows-by-cols grid, initially moving east.

List Grid Cells Encountered by an Expanding Clockwise Spiral

Company: Bloomberg

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Start at `(rStart,cStart)` in a zero-indexed rows-by-cols grid, initially moving east. Follow the expanding clockwise spiral: east1,south1,west2,north2,east3,south3, and so on. Continue walking when outside the grid, but record only in-grid coordinates. Return all grid coordinates in their encounter order, including the starting cell. ```cpp class Solution { public: vector<vector<int>> spiralMatrixIII(int rows, int cols, int rStart, int cStart); }; ``` ### Examples **Example 1:** ```text Input: rows = 2, cols = 2, rStart = 0, cStart = 0 Output: [[0,0],[0,1],[1,1],[1,0]] Explanation: The initial east,south,and west legs visit the other three grid cells in that order. ``` **Example 2:** ```text Input: rows = 1, cols = 3, rStart = 0, cStart = 1 Output: [[0,1],[0,2],[0,0]] Explanation: The walk visits the right cell first, leaves the row, then reaches the left cell on its first northward leg. ``` ### Constraints - `1 <= rows,cols <= 100`. - `0 <= rStart < rows`. - `0 <= cStart < cols`. ### Contract considerations The walk is defined on the whole plane; the grid is only a filter on recorded coordinates. ```hint Read the boundary condition The leg lengths increase after every second turn, not after every turn. ``` ### Boundary reminders The walk is defined on the whole plane; the grid is only a filter on recorded coordinates.

Overview: List Grid Cells Encountered by an Expanding Clockwise Spiral. Start at (rStart,cStart) in a zero-indexed rows-by-cols grid, initially moving east.

Bloomberg logo
Bloomberg
Jun 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Start at (rStart,cStart) in a zero-indexed rows-by-cols grid, initially moving east. Follow the expanding clockwise spiral: east1,south1,west2,north2,east3,south3, and so on. Continue walking when outside the grid, but record only in-grid coordinates. Return all grid coordinates in their encounter order, including the starting cell.

class Solution {
public:
    vector<vector<int>> spiralMatrixIII(int rows, int cols, int rStart, int cStart);
};

Examples

Example 1:

Input: rows = 2, cols = 2, rStart = 0, cStart = 0
Output: [[0,0],[0,1],[1,1],[1,0]]
Explanation: The initial east,south,and west legs visit the other three grid cells in that order.

Example 2:

Input: rows = 1, cols = 3, rStart = 0, cStart = 1
Output: [[0,1],[0,2],[0,0]]
Explanation: The walk visits the right cell first, leaves the row, then reaches the left cell on its first northward leg.

Constraints

  • 1 <= rows,cols <= 100 .
  • 0 <= rStart < rows .
  • 0 <= cStart < cols .

Contract considerations

The walk is defined on the whole plane; the grid is only a filter on recorded coordinates.

Boundary reminders

The walk is defined on the whole plane; the grid is only a filter on recorded coordinates.