Startup Interview Coding Question: Spiral Matrix III

14 Views
No Comments

You are given a matrix whose elements are initially set to 0 and a starting position (startRow, startCol) (0-based indexing).

Fill the matrix with increasing integers starting from 1 in a clockwise spiral pattern, beginning at the given starting position.

The spiral follows this direction order: rightdownleftup. Whenever the next position would go outside the matrix boundary or reach a previously filled cell, turn clockwise and continue.

This problem asks you to start from a given cell and fill the matrix with consecutive numbers in a clockwise spiral: right, down, left, and up. The main idea is to simulate movement step by step, using a direction array and turning whenever the next position is out of bounds or already filled. Careful boundary checking and visited-state tracking are the key implementation details.

END
 0