You start at the cell (rStart, cStart) of an rows x 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.
Example 1:
Input: rows = 1, cols = 4, rStart = 0, cStart = 0 Output: [[0,0],[0,1],[0,2],[0,3]]
Example 2:
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]]
Constraints:
1 <= rows, cols <= 1000 <= rStart < rows0 <= cStart < colsIn #885 Spiral Matrix III, you are given a grid size and a starting position. The goal is to output coordinates of cells visited while walking in a spiral pattern (east → south → west → north) until all grid cells are covered. The key challenge is that the spiral may temporarily move outside the grid boundaries, but only valid coordinates should be recorded.
A common approach is to simulate the spiral movement. Maintain a direction list and gradually increase the number of steps taken. After every two direction changes, increase the step length (e.g., 1 step east, 1 south, 2 west, 2 north, 3 east...). During movement, check whether the current coordinate is inside the grid; if so, add it to the result list.
This simulation continues until rows * cols valid coordinates are collected. The approach ensures each cell is added exactly once while following the spiral pattern. The algorithm runs in O(R × C) time since every grid cell is recorded once, with O(1) auxiliary space excluding the output.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Spiral Simulation with Expanding Steps | O(R × C) | O(1) auxiliary (O(R × C) for output) |
take U forward
To simulate walking in a spiral, maintain a direction vector and a step count for each segment. Initially face east, and keep track of direction changes in the order of east, south, west, north. After two direction changes, increase the step count which determines how far to walk in the current direction before turning.
Time Complexity: O(rows * cols), since each cell is visited once,
Space Complexity: O(rows * cols), for the result storage.
1#include <vector>
2
3using namespace std;
4
5vector<vector<int>> spiralMatrixIII(int rows, int cols, int rStart, int cStart) {
6 vector<vector<int>> result;
7 result.push_back({rStart, cStart});
8 int numSteps = 1;
9 while (result.size() < rows * cols) {
10 for (int i = 0; i < 2; ++i) {
11 for (int j = 0; j < numSteps; ++j) {
12 if (result.size() == rows * cols) return result;
13 if (i == 0) cStart++;
14 if (i == 1) rStart++;
15 if (rStart >= 0 && rStart < rows && cStart >= 0 && cStart < cols) {
16 result.push_back({rStart, cStart});
17 }
18 }
19 }
20 numSteps++;
21 for (int i = 0; i < 2; ++i) {
22 for (int j = 0; j < numSteps; ++j) {
23 if (result.size() == rows * cols) return result;
24 if (i == 0) cStart--;
25 if (i == 1) rStart--;
26 if (rStart >= 0 && rStart < rows && cStart >= 0 && cStart < cols) {
27 result.push_back({rStart, cStart});
28 }
29 }
30 }
31 numSteps++;
32 }
33 return result;
34}This solution involves incrementing/decrementing rows and columns while checking whether they're within bounds. The number of steps increases after every two directions.
Watch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
Yes, spiral traversal and grid simulation problems are common in technical interviews, including FAANG-style interviews. They test understanding of matrix traversal, boundary handling, and simulation logic.
The problem mainly relies on simple variables and a list to store coordinates. A direction array and step counter are typically used to control movement in the spiral pattern efficiently.
The optimal approach is to simulate spiral movement starting from the given cell. By moving in four directions and increasing step lengths after every two turns, you can generate the correct spiral order while collecting valid grid coordinates.
The spiral grows outward as you move away from the starting cell. Increasing the step size after every two directions ensures the path expands evenly and forms the correct spiral pattern.