Challenge - 5 Problems
Spiral Matrix Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Spiral Traversal on 3x3 Matrix
What is the output of the spiral traversal on the following 3x3 matrix?
Matrix:
1 2 3
4 5 6
7 8 9
Matrix:
1 2 3
4 5 6
7 8 9
DSA Go
matrix := [][]int{{1,2,3},{4,5,6},{7,8,9}} result := SpiralTraverse(matrix) fmt.Println(result)
Attempts:
2 left
💡 Hint
Start from the top-left corner and move right, then down, then left, then up, shrinking the boundaries each time.
✗ Incorrect
The spiral traversal starts at 1, moves right to 3, then down to 9, left to 7, up to 4, and finally the center 5.
❓ Predict Output
intermediateSpiral Traversal Output for 2x4 Matrix
What is the output of spiral traversal on this 2x4 matrix?
Matrix:
1 2 3 4
5 6 7 8
Matrix:
1 2 3 4
5 6 7 8
DSA Go
matrix := [][]int{{1,2,3,4},{5,6,7,8}} result := SpiralTraverse(matrix) fmt.Println(result)
Attempts:
2 left
💡 Hint
Traverse right across the top row, then down the last column, then left across the bottom row.
✗ Incorrect
The spiral traversal goes 1 2 3 4 (top row), then 8 (down last column), then 7 6 5 (bottom row reversed).
🔧 Debug
advancedIdentify the Bug in Spiral Traversal Code
Given this Go code snippet for spiral traversal, what is the bug causing incorrect output?
DSA Go
func SpiralTraverse(matrix [][]int) []int { var result []int top, bottom := 0, len(matrix)-1 left, right := 0, len(matrix[0])-1 for top <= bottom && left <= right { for i := left; i <= right; i++ { result = append(result, matrix[top][i]) } top++ for i := top; i <= bottom; i++ { result = append(result, matrix[i][right]) } right-- if top <= bottom { for i := right; i >= left; i-- { result = append(result, matrix[bottom][i]) } bottom-- } if left <= right { for i := bottom; i >= top; i-- { result = append(result, matrix[i][left]) } left++ } } return result }
Attempts:
2 left
💡 Hint
Check if the boundaries have crossed before traversing bottom row and left column inside the loop.
✗ Incorrect
After moving top and right boundaries, bottom and left may cross top and right, so we must check before traversing bottom row and left column to avoid duplicates.
🧠 Conceptual
advancedTime Complexity of Spiral Matrix Traversal
What is the time complexity of performing a spiral traversal on an MxN matrix?
Attempts:
2 left
💡 Hint
Think about how many times each element is processed during traversal.
✗ Incorrect
Each element in the matrix is visited exactly once in spiral traversal, so the time complexity is proportional to the total number of elements M*N.
🚀 Application
expertSpiral Traversal Output for Non-Rectangular Matrix
Given this jagged matrix (rows of different lengths), what is the output of spiral traversal if implemented naively without boundary checks?
Matrix:
[ [1, 2, 3],
[4, 5],
[6, 7, 8, 9] ]
Matrix:
[ [1, 2, 3],
[4, 5],
[6, 7, 8, 9] ]
DSA Go
matrix := [][]int{{1,2,3},{4,5},{6,7,8,9}} result := SpiralTraverse(matrix) fmt.Println(result)
Attempts:
2 left
💡 Hint
Consider what happens when the code tries to access elements beyond the length of shorter rows.
✗ Incorrect
Naive spiral traversal assumes all rows have equal length. Accessing missing elements in shorter rows causes runtime index out of range error.
