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 following C code that prints the elements of a 3x3 matrix in spiral order?
DSA C
int matrix[3][3] = {{1,2,3},{4,5,6},{7,8,9}}; int i, k = 0, l = 0, m = 3, n = 3; while (k < m && l < n) { for (i = l; i < n; ++i) printf("%d ", matrix[k][i]); k++; for (i = k; i < m; ++i) printf("%d ", matrix[i][n-1]); n--; if (k < m) { for (i = n-1; i >= l; --i) printf("%d ", matrix[m-1][i]); m--; } if (l < n) { for (i = m-1; i >= k; --i) printf("%d ", matrix[i][l]); l++; } }
Attempts:
2 left
💡 Hint
Follow the spiral path: top row left to right, right column top to bottom, bottom row right to left, left column bottom to top.
✗ Incorrect
The code prints the outer layer first: 1 2 3 6 9 8 7 4, then the inner element 5.
❓ Predict Output
intermediate2:00remaining
Spiral Traversal Output for 2x4 Matrix
What is the output of the spiral traversal for this 2x4 matrix?
DSA C
int matrix[2][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}}; int i, k = 0, l = 0, m = 2, n = 4; while (k < m && l < n) { for (i = l; i < n; ++i) printf("%d ", matrix[k][i]); k++; for (i = k; i < m; ++i) printf("%d ", matrix[i][n-1]); n--; if (k < m) { for (i = n-1; i >= l; --i) printf("%d ", matrix[m-1][i]); m--; } if (l < n) { for (i = m-1; i >= k; --i) printf("%d ", matrix[i][l]); l++; } }
Attempts:
2 left
💡 Hint
Remember to traverse the top row, then right column, then bottom row in reverse, then left column in reverse.
✗ Incorrect
The spiral order is: 1 2 3 4 (top row), 8 (right column), 7 6 5 (bottom row reversed).
🔧 Debug
advanced2:00remaining
Identify the Bug in Spiral Traversal Code
The following code is intended to print a matrix in spiral order but produces incorrect output. What is the bug?
DSA C
int matrix[3][3] = {{1,2,3},{4,5,6},{7,8,9}}; int i, k = 0, l = 0, m = 3, n = 3; while (k <= m && l <= n) { for (i = l; i < n; ++i) printf("%d ", matrix[k][i]); k++; for (i = k; i < m; ++i) printf("%d ", matrix[i][n-1]); n--; if (k < m) { for (i = n-1; i >= l; --i) printf("%d ", matrix[m-1][i]); m--; } if (l < n) { for (i = m-1; i >= k; --i) printf("%d ", matrix[i][l]); l++; } }
Attempts:
2 left
💡 Hint
Check the loop condition carefully to avoid out-of-bound access.
✗ Incorrect
Using '<=' causes the loops to run one extra time, leading to invalid access and wrong output.
🧠 Conceptual
advanced1:30remaining
Number of Layers in Spiral Traversal
For an MxN matrix, how many layers (or rings) does the spiral traversal process?
Attempts:
2 left
💡 Hint
Each layer removes one row and one column from each side.
✗ Incorrect
Each layer covers the outer ring, so the number of layers is half the smaller dimension rounded down.
🚀 Application
expert2:30remaining
Spiral Traversal Output for 4x4 Matrix
What is the spiral traversal output of this 4x4 matrix?
DSA C
int matrix[4][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16} }; int i, k = 0, l = 0, m = 4, n = 4; while (k < m && l < n) { for (i = l; i < n; ++i) printf("%d ", matrix[k][i]); k++; for (i = k; i < m; ++i) printf("%d ", matrix[i][n-1]); n--; if (k < m) { for (i = n-1; i >= l; --i) printf("%d ", matrix[m-1][i]); m--; } if (l < n) { for (i = m-1; i >= k; --i) printf("%d ", matrix[i][l]); l++; } }
Attempts:
2 left
💡 Hint
Trace the spiral layer by layer: top row, right column, bottom row reversed, left column reversed.
✗ Incorrect
The spiral order covers the outer ring first, then the inner 2x2 matrix in spiral order.
