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 following C# code that prints elements of a 3x3 matrix in spiral order?
DSA C#
int[,] matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; List<int> result = new List<int>(); int top = 0, bottom = 2, left = 0, right = 2; while (top <= bottom && left <= right) { for (int i = left; i <= right; i++) result.Add(matrix[top, i]); top++; for (int i = top; i <= bottom; i++) result.Add(matrix[i, right]); right--; if (top <= bottom) { for (int i = right; i >= left; i--) result.Add(matrix[bottom, i]); bottom--; } if (left <= right) { for (int i = bottom; i >= top; i--) result.Add(matrix[i, left]); left++; } } Console.WriteLine(string.Join(", ", result));
Attempts:
2 left
💡 Hint
Follow the boundaries top, bottom, left, right and move inward in spiral.
✗ Incorrect
The code traverses the matrix in layers: top row left to right, right column top to bottom, bottom row right to left, left column bottom to top, then moves inward. This produces the spiral order: 1,2,3,6,9,8,7,4,5.
❓ Predict Output
intermediateSpiral Traversal Output for 2x4 Matrix
What is the output of the spiral traversal code on this 2x4 matrix?
DSA C#
int[,] matrix = { {1, 2, 3, 4}, {5, 6, 7, 8} }; List<int> result = new List<int>(); int top = 0, bottom = 1, left = 0, right = 3; while (top <= bottom && left <= right) { for (int i = left; i <= right; i++) result.Add(matrix[top, i]); top++; for (int i = top; i <= bottom; i++) result.Add(matrix[i, right]); right--; if (top <= bottom) { for (int i = right; i >= left; i--) result.Add(matrix[bottom, i]); bottom--; } if (left <= right) { for (int i = bottom; i >= top; i--) result.Add(matrix[i, left]); left++; } } Console.WriteLine(string.Join(", ", result));
Attempts:
2 left
💡 Hint
Traverse top row, then right column, then bottom row reversed, then left column reversed.
✗ Incorrect
The spiral order for this 2x4 matrix is: top row (1,2,3,4), right column down (8), bottom row reversed (7,6,5), left column up (none left). So output is 1,2,3,4,8,7,6,5.
🔧 Debug
advancedIdentify the Bug in Spiral Traversal Code
The following code attempts spiral traversal but produces incorrect output. What is the bug?
DSA C#
int[,] matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; List<int> result = new List<int>(); int top = 0, bottom = 2, left = 0, right = 2; while (top < bottom && left < right) { for (int i = left; i <= right; i++) result.Add(matrix[top, i]); top++; for (int i = top; i <= bottom; i++) result.Add(matrix[i, right]); right--; for (int i = right; i >= left; i--) result.Add(matrix[bottom, i]); bottom--; for (int i = bottom; i >= top; i--) result.Add(matrix[i, left]); left++; } Console.WriteLine(string.Join(", ", result));
Attempts:
2 left
💡 Hint
Check the loop condition carefully to ensure all rows and columns are included.
✗ Incorrect
Using 'top < bottom && left < right' excludes the last row and column when top == bottom or left == right. Changing to 'top <= bottom && left <= right' ensures all elements are covered.
🧠 Conceptual
advancedNumber of Layers in Spiral Traversal
For an MxN matrix, how many layers (or rings) does the spiral traversal algorithm process?
Attempts:
2 left
💡 Hint
Each layer removes one row from top and bottom and one column from left and right.
✗ Incorrect
Each layer corresponds to one ring around the matrix. The number of layers is half the smaller dimension rounded down because each layer consumes two rows or columns.
🚀 Application
expertFinal Spiral Traversal Output for 4x4 Matrix
What is the output of spiral traversal on this 4x4 matrix?
DSA C#
int[,] matrix = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16} }; List<int> result = new List<int>(); int top = 0, bottom = 3, left = 0, right = 3; while (top <= bottom && left <= right) { for (int i = left; i <= right; i++) result.Add(matrix[top, i]); top++; for (int i = top; i <= bottom; i++) result.Add(matrix[i, right]); right--; if (top <= bottom) { for (int i = right; i >= left; i--) result.Add(matrix[bottom, i]); bottom--; } if (left <= right) { for (int i = bottom; i >= top; i--) result.Add(matrix[i, left]); left++; } } Console.WriteLine(string.Join(", ", result));
Attempts:
2 left
💡 Hint
Follow the spiral from outer layer to inner layer carefully.
✗ Incorrect
The spiral order is top row left to right (1,2,3,4), right column top to bottom (8,12,16), bottom row right to left (15,14,13), left column bottom to top (9,5), then inner layer top row (6,7), right column (11), bottom row reversed (10).
