FunctionRacerProblemsRace

Spiral Order

MediumGridsPython, JavaScript, TypeScript, Java, C++

Walk through matrix in a clockwise spiral: start at the top-left cell, go right along the top row, then down the right column, then left along the bottom row, then up, and keep spiralling inward.

Return the numbers in the order you visit them. The grid may be any rectangle.

Example 1

matrix [[1,2,3],[4,5,6],[7,8,9]]
returns [1,2,3,6,9,8,7,4,5]

1, 2, 3, then 6, 9, then 8, 7, then 4, then the centre 5.

Example 2

matrix [[1,2,3,4],[5,6,7,8]]
returns [1,2,3,4,8,7,6,5]

Right along row 0, then back along row 1: 1,2,3,4,8,7,6,5.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Grids problems