Bird
0
0
DSA Cprogramming~20 mins

Spiral Matrix Traversal in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Spiral Matrix Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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++;
    }
}
A[1, 2, 3, 6, 9, 7, 8, 4, 5]
B[1, 2, 3, 6, 9, 8, 7, 4, 5]
C[1, 2, 3, 6, 9, 8, 7, 5, 4]
D[1, 2, 3, 5, 6, 9, 8, 7, 4]
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.
Predict Output
intermediate
2: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++;
    }
}
A[1, 2, 3, 4, 8, 7, 6]
B[1, 2, 3, 4, 5, 6, 7, 8]
C[1, 2, 3, 4, 8, 7, 6, 5]
D[1, 2, 3, 4, 8, 7, 5, 6]
Attempts:
2 left
💡 Hint
Remember to traverse the top row, then right column, then bottom row in reverse, then left column in reverse.
🔧 Debug
advanced
2: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++;
    }
}
AThe matrix indices are accessed incorrectly; rows and columns are swapped.
BThe inner loops should use '<=' instead of '<' for correct indexing.
CThe variables m and n should be incremented inside the loop instead of decremented.
DThe loop condition should be 'k < m && l < n' instead of 'k <= m && l <= n'.
Attempts:
2 left
💡 Hint
Check the loop condition carefully to avoid out-of-bound access.
🧠 Conceptual
advanced
1:30remaining
Number of Layers in Spiral Traversal
For an MxN matrix, how many layers (or rings) does the spiral traversal process?
Amin(M, N) / 2 rounded down
Bmax(M, N) / 2 rounded down
CM + N - 1
Dmin(M, N)
Attempts:
2 left
💡 Hint
Each layer removes one row and one column from each side.
🚀 Application
expert
2: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++;
    }
}
A[1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5, 6, 7, 11, 10]
B[1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5, 6, 10, 11, 7]
C[1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5, 10, 11, 7, 6]
D[1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5, 7, 6, 10, 11]
Attempts:
2 left
💡 Hint
Trace the spiral layer by layer: top row, right column, bottom row reversed, left column reversed.