Clockwise Spiral Walk of a Grid
Updated
Problem statement
You are given a grid of integers called matrix, with some number of rows and columns. List all of its values in spiral order, and return them as one list.
The walk starts at the top-left cell. It goes right along the top row, then down the last column, then left along the bottom row, then up the first column. After that it turns clockwise again and does the same on the part of the grid that is still unread, and so on until every cell has been read exactly once.
Function
spiralOrder(matrix: int[][]) → List<Integer>Examples
Example 1
matrix = [[8,-3,14,6],[21,0,-9,5],[17,2,30,-1]]return = [8,-3,14,6,5,-1,30,2,17,21,0,-9]The grid has 3 rows and 4 columns. Go right along the top row: 8, -3, 14, 6. Go down the right column: 5, -1. Go left along the bottom row: 30, 2, 17. Go up the left column: 21. The inside is one row with 0, -9, so the walk ends with those two values.
Example 2
matrix = [[7],[-2],[19],[4]]return = [7,-2,19,4]The grid has one column, so the walk can only go down. Each value is read once, from the top to the bottom.
Example 3
matrix = [[12,5,40],[9,66,3],[-8,21,17],[50,1,33],[4,0,25]]return = [12,5,40,3,17,33,25,0,4,50,-8,9,66,21,1]The grid has 5 rows and 3 columns. The outer ring is read first: the top row 12, 5, 40, the right column 3, 17, 33, 25, the bottom row from right to left 0, 4, and the left column going up 50, -8, 9. The inside is one column with 66, 21, 1, read from top to bottom.
Constraints
1 <= matrix.length <= 50and1 <= matrix[i].length <= 50- All rows have the same length.
-1000 <= matrix[i][j] <= 1000
More Expedia Onsite Interview questions
- Scramble StringONSITE INTERVIEW · Seen Sep 2026
- Smallest Sufficient TeamONSITE INTERVIEW · Seen Sep 2026
- Check Anagram Pairs Index by IndexONSITE INTERVIEW · Seen Oct 2025
- Fresh Oranges Left After Rotting StopsONSITE INTERVIEW · Seen Oct 2025
- Longest Increasing SubsequenceONSITE INTERVIEW · Seen Oct 2025
- Lowest Common Ancestor with Repeated and Missing ValuesONSITE INTERVIEW · Seen Oct 2025
- Shortest Substring with Exactly K OnesONSITE INTERVIEW · Seen Oct 2025
- Middle Element of a Linked ListONSITE INTERVIEW · Seen Nov 2024
- Minimum in a Rotated Sorted ArrayONSITE INTERVIEW · Seen Sep 2024
- Mirror Image Binary TreesONSITE INTERVIEW · Seen Sep 2024
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
Expedia interview experiences
- Expedia Intern Final Round in 2021: 28 Experiences (Part 4 of 6)
- Expedia Intern Finals Before 2020: 14 Experiences (Part 6 of 6)
- Expedia Intern Final Round in 2020: 24 Experiences (Part 5 of 6)
- Expedia Intern Final Round in 2022: 15 Experiences (Part 3 of 6)
- Expedia Intern Final Round in 2023: 17 Experiences (Part 2 of 6)
- Expedia SWE Intern Final Round: 126 Experiences (Part 1 of 6)