Bird
Raised Fist0
DSA Cprogramming~20 mins

Spiral Matrix Traversal in DSA C - Practice Problems & Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main idea behind Spiral Matrix Traversal?
easy
A. Traverse only the diagonal elements of the matrix
B. Traverse the matrix row by row from top to bottom
C. Traverse the matrix column by column from left to right
D. Traverse the matrix in a circular pattern from outer to inner layers

Solution

  1. Step 1: Understand traversal pattern

    Spiral traversal means moving around the edges of the matrix and gradually moving inward in a circular way.
  2. Step 2: Compare options

    Only Traverse the matrix in a circular pattern from outer to inner layers describes moving in a circular pattern from outer to inner layers, which matches spiral traversal.
  3. Final Answer:

    Traverse the matrix in a circular pattern from outer to inner layers -> Option D
  4. Quick Check:

    Spiral traversal = circular outer to inner [OK]
Hint: Think of peeling an onion layer by layer [OK]
Common Mistakes:
  • Confusing spiral with row-wise traversal
  • Thinking spiral means diagonal traversal
  • Ignoring the inward movement after outer layer
2. Which of the following C code snippets correctly initializes variables for spiral traversal of an m x n matrix?
easy
A. int top = 0, bottom = m-1, left = 0, right = n-1;
B. int top = m-1, bottom = 0, left = n-1, right = 0;
C. int top = 0, bottom = n-1, left = 0, right = m-1;
D. int top = n-1, bottom = m-1, left = 0, right = 0;

Solution

  1. Step 1: Identify correct boundary initialization

    For spiral traversal, top starts at 0, bottom at last row (m-1), left at 0, right at last column (n-1).
  2. Step 2: Match with options

    int top = 0, bottom = m-1, left = 0, right = n-1; matches this initialization exactly; others swap or mix row and column indices incorrectly.
  3. Final Answer:

    int top = 0, bottom = m-1, left = 0, right = n-1; -> Option A
  4. Quick Check:

    Boundaries = top=0, bottom=m-1, left=0, right=n-1 [OK]
Hint: Top and left start at 0; bottom and right at max index [OK]
Common Mistakes:
  • Swapping m and n in bottom or right
  • Starting top or left at max index
  • Confusing row and column boundaries
3. What is the output of the following C code snippet for spiral traversal of a 3x3 matrix?
int matrix[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
int top=0, bottom=2, left=0, right=2;
while(top <= bottom && left <= right) {
  for(int i=left; i<=right; i++) printf("%d ", matrix[top][i]);
  top++;
  for(int i=top; i<=bottom; i++) printf("%d ", matrix[i][right]);
  right--;
  if(top <= bottom) {
    for(int i=right; i>=left; i--) printf("%d ", matrix[bottom][i]);
    bottom--;
  }
  if(left <= right) {
    for(int i=bottom; i>=top; i--) printf("%d ", matrix[i][left]);
    left++;
  }
}
medium
A. 1 4 7 8 9 6 3 2 5
B. 1 2 3 6 9 8 7 4 5
C. 1 2 3 4 5 6 7 8 9
D. 1 3 5 7 9 2 4 6 8

Solution

  1. Step 1: Trace first outer layer traversal

    Top row left to right: 1 2 3; then right column top to bottom: 6 9; bottom row right to left: 8 7; left column bottom to top: 4.
  2. Step 2: Move inward and print center

    Now top=1, bottom=1, left=1, right=1; print center element matrix[1][1] = 5.
  3. Final Answer:

    1 2 3 6 9 8 7 4 5 -> Option B
  4. Quick Check:

    Spiral order = 1 2 3 6 9 8 7 4 5 [OK]
Hint: Follow boundaries and print edges clockwise [OK]
Common Mistakes:
  • Skipping center element
  • Mixing row and column indices
  • Not updating boundaries correctly
4. Find the error in this spiral traversal code snippet for a 3x3 matrix:
int top=0, bottom=2, left=0, right=2;
while(top < bottom && left < right) {
  for(int i=left; i<=right; i++) printf("%d ", matrix[top][i]);
  top++;
  for(int i=top; i<=bottom; i++) printf("%d ", matrix[i][right]);
  right--;
  for(int i=right; i>=left; i--) printf("%d ", matrix[bottom][i]);
  bottom--;
  for(int i=bottom; i>=top; i--) printf("%d ", matrix[i][left]);
  left++;
}
medium
A. The variables top and bottom are swapped
B. The for loops should use < instead of <=
C. The while loop condition should use <= instead of <
D. The matrix indices are accessed incorrectly

Solution

  1. Step 1: Analyze loop condition

    The condition top < bottom && left < right stops before the center element is processed in odd-sized matrices.
  2. Step 2: Correct condition for full traversal

    Using <= allows the loop to run when top equals bottom or left equals right, ensuring center elements are included.
  3. Final Answer:

    The while loop condition should use <= instead of < -> Option C
  4. Quick Check:

    Use <= in while condition for complete spiral [OK]
Hint: Use <= in while to include center elements [OK]
Common Mistakes:
  • Stopping traversal too early
  • Changing for loop bounds incorrectly
  • Swapping top and bottom variables
5. Given a 4x4 matrix:
int matrix[4][4] = {
  {1, 2, 3, 4},
  {5, 6, 7, 8},
  {9, 10, 11, 12},
  {13, 14, 15, 16}
};

Write the spiral traversal output if you start from the bottom-left corner and move clockwise inward.
hard
A. 13 9 5 1 2 3 4 8 12 16 15 14 10 6 7 11
B. 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
C. 13 14 15 16 12 8 4 3 2 1 5 9 10 11 7 6
D. 16 15 14 13 9 5 1 2 3 4 8 12 11 10 6 7

Solution

  1. Step 1: Understand starting point and direction

    Start at bottom-left (matrix[3][0] = 13) and move clockwise: up, right, down, left, inward.
  2. Step 2: Trace spiral clockwise from bottom-left

    Up column: 13 9 5 1; right row: 2 3 4; down column: 8 12 16; left row: 15 14 10 6; then inner spiral: 7 11.
  3. Final Answer:

    13 9 5 1 2 3 4 8 12 16 15 14 10 6 7 11 -> Option A
  4. Quick Check:

    Start bottom-left clockwise spiral = 13 9 5 1 2 3 4 8 12 16 15 14 10 6 7 11 [OK]
Hint: Change start and direction, then trace edges inward [OK]
Common Mistakes:
  • Starting from top-left instead of bottom-left
  • Moving counter-clockwise instead of clockwise
  • Mixing row and column indices
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard