Spiral Matrix
Updated
Problem statement
Given an m x n matrix matrix, return all of its elements in spiral order.
Spiral order starts at the top-left element and goes clockwise: right along the top row, down the right column, left along the bottom row, and up the left column. It then does the same with the part of the matrix that is still left inside, until every element has been listed exactly once.
Function
spiralOrder(matrix: int[][]) → List<Integer>Examples
Example 1
matrix = [[1,2,3],[4,5,6],[7,8,9]]return = [1,2,3,6,9,8,7,4,5]The outer ring, read clockwise from the top-left corner, is 1, 2, 3, 6, 9, 8, 7, 4. Only 5 is left in the middle, so it comes last.
Example 2
matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]return = [1,2,3,4,8,12,11,10,9,5,6,7]The outer ring gives 1, 2, 3, 4 along the top, 8, 12 down the right, 11, 10, 9 along the bottom and 5 up the left. What is left inside is the single row 6, 7, read left to right.
Example 3
matrix = [[7],[9],[6]]return = [7,9,6]The matrix is one column. The top row is just 7, and going down the right column adds 9 and 6. There is no separate bottom row or left column to read back.
Constraints
m == matrix.lengthn == matrix[i].length1 <= m, n <= 10-100 <= matrix[i][j] <= 100
More Athenahealth interview questions
Similar questions at other companies
- Spiral MatrixNike, Inc. · ONSITE INTERVIEWNike, Inc. interview questions
- Incremental Hard Attention with a KV CacheOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Stochastic Gradient Descent with MomentumMeta · ONSITE INTERVIEWMeta interview questions
- Minesweeper Board UpdateUber · ONSITE INTERVIEWUber interview questions
- Game of Life: In-Place Next GenerationGoogle · ONSITE INTERVIEWGoogle interview questions
- Diagonal Matrix IteratorMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Candy Crush Board Until StableBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
- Game of Life Next GenerationTemporal · ONSITE INTERVIEWTemporal interview questions
- Minesweeper Board UpdateClay · ONSITE INTERVIEW
- Minesweeper Board UpdateAirbyte · ONSITE INTERVIEW