Spiral Matrix Order
Updated
Problem statement
You are given an m x n matrix of integers, matrix. Return all of its values in clockwise spiral order.
The spiral starts with the value in the upper-left corner and moves right along the first row to its end. Then it goes down the last column, left along the last row, and up the first column. That finishes the outer ring. The spiral then goes through the next ring inside in the same way, and so on, until every value has been taken exactly once.
If a ring is only one row tall, take its values once from left to right. If it is only one column wide, take its values once from top to bottom.
Function
spiralOrder(matrix: int[][]) → List<Integer>Examples
Example 1
matrix = [[4,9,2],[3,5,7],[8,1,6]]return = [4,9,2,7,6,1,8,3,5]The outer ring gives the top row 4, 9, 2, then 7, 6 going down the right column, then 1, 8 going left along the bottom row, then 3 going up the left column. The only value left is the center, 5.
Example 2
matrix = [[1,-2,3],[-4,5,-6],[7,-8,9],[-10,11,-12]]return = [1,-2,3,-6,9,-12,11,-10,7,-4,5,-8]The outer ring gives 1, -2, 3 along the top, -6, 9, -12 down the right column, 11, -10 along the bottom and 7, -4 up the left column. What is left is the middle column of rows 1 and 2 (counting from 0), a ring that is only one column wide, so it is taken once from top to bottom: 5, -8.
Example 3
matrix = [[5],[0],[-5]]return = [5,0,-5]The matrix is a single column, so its values are taken once from top to bottom.
Constraints
m == matrix.lengthn == matrix[i].length1 <= m, n <= 50-1000 <= matrix[i][j] <= 1000
More Airbnb Phone Screen questions
- Read Across Sound TracksPHONE SCREEN · Seen Nov 2026
- Text JustificationPHONE SCREEN · Seen Nov 2026
- Decode and Store StringsPHONE SCREEN · Seen Oct 2026
- Print Sentences as TablePHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Detonate Bombs with Chain ReactionsPHONE SCREEN · Seen Aug 2026
- Smallest Common RegionPHONE SCREEN · Seen Aug 2026
- Longest Chain BookingPHONE SCREEN · Seen Aug 2026
- Nested List Iterator with RemovePHONE SCREEN · Seen Jul 2026
- Parse Query StringPHONE SCREEN · Seen May 2026
- Pacific Atlantic Water FlowPHONE SCREEN · Seen May 2023
Similar questions at other companies
- 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
- S-Shaped Matrix TraversalFox Corporation · PHONE SCREEN
- Longest Consecutive Same-Color LineGoogle · PHONE SCREENGoogle interview questions
- Process Minesweeper Game OperationsAmperity · PHONE SCREENAmperity interview questions
- Diagonal TraverseHive · PHONE SCREENHive interview questions
- Build a Bird’s-Eye-View Height GridNVIDIA · PHONE SCREENNVIDIA interview questions
- Multi-Channel Valid ConvolutionTesla · PHONE SCREENTesla interview questions
Interview reports that mention this question
Airbnb interview experiences
- Airbnb SWE OA: Four-Level Banking System: 1 Experience
- Airbnb Campus and Full-Time SWE OA / Interviews: 9 Experiences
- Airbnb Intern OA and Phone Screens: 17 Experiences (Part 6 of 7)
- Airbnb Intern OA and Onsite Rounds: 16 Experiences (Part 5 of 7)
- Airbnb Intern OA, Diplomacy Game: 19 Experiences (Part 4 of 7)