Challenge - 5 Problems
Spiral Matrix Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output 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 Python
def spiral_traversal(matrix): result = [] top, bottom = 0, len(matrix) - 1 left, right = 0, len(matrix[0]) - 1 while left <= right and top <= bottom: for i in range(left, right + 1): result.append(matrix[top][i]) top += 1 for i in range(top, bottom + 1): result.append(matrix[i][right]) right -= 1 if top <= bottom: for i in range(right, left - 1, -1): result.append(matrix[bottom][i]) bottom -= 1 if left <= right: for i in range(bottom, top - 1, -1): result.append(matrix[i][left]) left += 1 return result print(spiral_traversal([[1,2,3],[4,5,6],[7,8,9]]))
Attempts:
2 left
💡 Hint
Follow the edges of the matrix clockwise, moving inward layer by layer.
✗ Incorrect
The spiral traversal starts at the top-left corner and moves right, then down, then left, then up, shrinking the boundaries after each direction until all elements are visited.
❓ Predict Output
intermediate2:00remaining
Spiral Traversal Output for Rectangular Matrix
What is the spiral traversal output of this 3x4 matrix?
Matrix:
1 2 3 4
5 6 7 8
9 10 11 12
Matrix:
1 2 3 4
5 6 7 8
9 10 11 12
DSA Python
def spiral_traversal(matrix): result = [] top, bottom = 0, len(matrix) - 1 left, right = 0, len(matrix[0]) - 1 while left <= right and top <= bottom: for i in range(left, right + 1): result.append(matrix[top][i]) top += 1 for i in range(top, bottom + 1): result.append(matrix[i][right]) right -= 1 if top <= bottom: for i in range(right, left - 1, -1): result.append(matrix[bottom][i]) bottom -= 1 if left <= right: for i in range(bottom, top - 1, -1): result.append(matrix[i][left]) left += 1 return result print(spiral_traversal([[1,2,3,4],[5,6,7,8],[9,10,11,12]]))
Attempts:
2 left
💡 Hint
Traverse the outer layer first, then move inward.
✗ Incorrect
The traversal goes right across the top row, down the last column, left across the bottom row, and up the first column, then repeats for the inner layers.
🔧 Debug
advanced2:00remaining
Identify the Error in Spiral Traversal Code
The following code is intended to perform spiral traversal on a matrix. What error will it raise when run on a 1x1 matrix?
Matrix:
[[1]]
Matrix:
[[1]]
DSA Python
def spiral_traversal(matrix): result = [] top, bottom = 0, len(matrix) - 1 left, right = 0, len(matrix[0]) - 1 while left < right and top < bottom: for i in range(left, right + 1): result.append(matrix[top][i]) top += 1 for i in range(top, bottom + 1): result.append(matrix[i][right]) right -= 1 for i in range(right, left - 1, -1): result.append(matrix[bottom][i]) bottom -= 1 for i in range(bottom, top - 1, -1): result.append(matrix[i][left]) left += 1 return result print(spiral_traversal([[1]]))
Attempts:
2 left
💡 Hint
Check the loop condition for small matrices.
✗ Incorrect
The loop condition uses < instead of <=, so for 1x1 matrix the while loop never runs and returns empty list.
🚀 Application
advanced1:00remaining
Number of Elements Visited in Spiral Traversal
Given a 4x5 matrix, how many elements will be visited during a complete spiral traversal?
Attempts:
2 left
💡 Hint
Count all elements in the matrix.
✗ Incorrect
Spiral traversal visits every element exactly once, so total elements visited equals rows * columns = 4 * 5 = 20.
🧠 Conceptual
expert1:30remaining
Why Use Four Pointers in Spiral Traversal?
In spiral traversal of a matrix, why do we maintain four pointers (top, bottom, left, right)?
Attempts:
2 left
💡 Hint
Think about how the traversal shrinks the area it covers after each pass.
✗ Incorrect
The four pointers define the current edges of the matrix section that still needs to be traversed in spiral order. They move inward after each side is processed.