Spiral Matrix
Updated
Problem statement
Given an m x n matrix of integers, return all of its values in spiral order.
Spiral order starts at the top-left corner and walks clockwise around the outer edge: right along the top row, down the right column, left along the bottom row, then up the left column. The walk then does the same on the next ring inside and keeps moving inward until every value has been visited exactly once.
Function
spiralOrder(matrix: int[][]) → List<Integer>Examples
Example 1
matrix = [[1,2,3,4],[10,11,12,5],[9,8,7,6]]return = [1,2,3,4,5,6,7,8,9,10,11,12]The outer ring comes first: 1, 2, 3, 4 along the top row, 5 and 6 down the right column, 7, 8, 9 back along the bottom row, and 10 up the left column.
Only 11 and 12 in the middle row are left. They form the next ring and are read left to right. The values in this matrix were placed so that the answer counts from 1 to 12.
Example 2
matrix = [[5,-1],[0,7],[3,-4],[8,2]]return = [5,-1,7,-4,2,8,3,0]The top row gives 5 and -1. Going down the right column gives 7, -4 and 2. Going left along the bottom row gives 8, and going up the left column gives 3 and 0.
That covers all 8 values, so no inner ring is left.
Example 3
matrix = [[6],[-3],[0]]return = [6,-3,0]The matrix has one column. The top row gives 6, and the walk down the right column gives -3 and 0.
Every value is now taken, so the walk does not come back up the same column.
Constraints
m == matrix.lengthn == matrix[i].length1 <= m, n <= 10-100 <= matrix[i][j] <= 100
More Zillow Onsite Interview questions
- Valid ParenthesesPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2025
- Build a Hash TablePHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2023
- First Subarray with the Largest SumOA · ONSITE INTERVIEW · Seen Aug 2021
- Count Duplicate CharactersONSITE INTERVIEW · Seen Oct 2020
- Decision Tree Classifier from ScratchONSITE INTERVIEW · Seen Nov 2019
- Find Median from Data StreamPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2018
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2016
- Tic-Tac-Toe Winner on a Huge BoardPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2016
- Most Frequent Three-Page SequencesPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2016
- Maximum Path Sum in a GridONSITE INTERVIEW · Seen Apr 2016
Similar questions at other companies
- Spiral MatrixAthenahealth · ONSITE INTERVIEW
- Spiral MatrixNike, Inc. · ONSITE INTERVIEWNike, Inc. interview questions
- Apply a 2048 Board MoveGlean · PHONE SCREENGlean interview questions
- Spiral Order with ObstaclesTemu · PHONE SCREENTemu interview questions
- Incremental Hard Attention with a KV CacheOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Spiral Matrix TraversalByteDance · PHONE SCREENByteDance interview questions
- Stochastic Gradient Descent with MomentumMeta · ONSITE INTERVIEWMeta interview questions
- S-Shaped Matrix TraversalFox Corporation · PHONE SCREEN
- Longest Consecutive Same-Color LineGoogle · PHONE SCREENGoogle interview questions
- Process Minesweeper Game OperationsAmperity · PHONE SCREENAmperity interview questions