Bird
Raised Fist0
DSA Javaprogramming~20 mins

Spiral Matrix Traversal in DSA Java - 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 Java code that prints the elements of a 3x3 matrix in spiral order?
DSA Java
int[][] matrix = {
  {1, 2, 3},
  {4, 5, 6},
  {7, 8, 9}
};

List<Integer> result = new ArrayList<>();
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++;
    }
}
System.out.println(result);
A[1, 2, 3, 6, 9, 8, 7, 5, 4]
B[1, 2, 3, 6, 9, 7, 8, 4, 5]
C[1, 2, 3, 6, 9, 8, 7, 4, 5]
D[1, 2, 3, 6, 9, 8, 4, 7, 5]
Attempts:
2 left
💡 Hint
Follow the boundaries top, right, bottom, and left carefully in order.
❓ Predict Output
intermediate
2:00remaining
Spiral Traversal Output for 2x4 Matrix
What is the output of the spiral traversal on this 2x4 matrix?
DSA Java
int[][] matrix = {
  {1, 2, 3, 4},
  {5, 6, 7, 8}
};

List<Integer> result = new ArrayList<>();
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++;
    }
}
System.out.println(result);
A[1, 2, 3, 4, 8, 6, 7, 5]
B[1, 2, 3, 4, 8, 7, 5, 6]
C[1, 2, 3, 4, 7, 8, 6, 5]
D[1, 2, 3, 4, 8, 7, 6, 5]
Attempts:
2 left
💡 Hint
Remember to move boundaries inward after each side is processed.
🔧 Debug
advanced
2:00remaining
Identify the Error in Spiral Traversal Code
What error will this Java code produce when trying to print spiral traversal of a 3x3 matrix?
DSA Java
int[][] matrix = {
  {1, 2, 3},
  {4, 5, 6},
  {7, 8, 9}
};

List<Integer> result = new ArrayList<>();
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++;
    }
}
System.out.println(result);
AMissing elements in output due to loop condition
BArrayIndexOutOfBoundsException
CNo output, infinite loop
DSyntaxError due to missing semicolon
Attempts:
2 left
💡 Hint
Check the while loop condition carefully.
🧠 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
Cmin(M, N) rounded up
Dmax(M, N) rounded up
Attempts:
2 left
💡 Hint
Each layer removes one row and one column from each side.
🚀 Application
expert
2:30remaining
Final State of Boundaries After Full Spiral Traversal
Given a 4x4 matrix, after completing the spiral traversal, what are the final values of the boundary variables top, bottom, left, and right?
DSA Java
int M = 4, N = 4;
int top = 0, bottom = M - 1, left = 0, right = N - 1;

while (top <= bottom && left <= right) {
    for (int i = left; i <= right; i++) {/* traverse top row */}
    top++;
    for (int i = top; i <= bottom; i++) {/* traverse right column */}
    right--;
    if (top <= bottom) {
        for (int i = right; i >= left; i--) {/* traverse bottom row */}
        bottom--;
    }
    if (left <= right) {
        for (int i = bottom; i >= top; i--) {/* traverse left column */}
        left++;
    }
}

System.out.println("top=" + top + ", bottom=" + bottom + ", left=" + left + ", right=" + right);
Atop=1, bottom=2, left=1, right=2
Btop=2, bottom=1, left=2, right=1
Ctop=2, bottom=2, left=2, right=2
Dtop=3, bottom=0, left=3, right=0
Attempts:
2 left
💡 Hint
Count how many times each boundary moves inward during traversal.

Practice

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

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 A
  4. Quick Check:

    Spiral traversal = circular outer to inner [OK]
Hint: Spiral means circle inward from edges [OK]
Common Mistakes:
  • Confusing spiral with row-wise traversal
  • Thinking spiral means diagonal only
  • Mixing column-wise with spiral
2. Which of the following Java code snippets correctly initializes the boundaries for spiral traversal of a matrix int[][] matrix?
easy
A. int top = 1, bottom = matrix.length, left = 1, right = matrix[0].length;
B. int top = 0, bottom = matrix.length - 1, left = 0, right = matrix[0].length - 1;
C. int top = 0, bottom = matrix[0].length - 1, left = 0, right = matrix.length - 1;
D. int top = matrix.length, bottom = 0, left = matrix[0].length, right = 0;

Solution

  1. Step 1: Identify matrix dimensions

    For a 2D array, rows = matrix.length, columns = matrix[0].length.
  2. Step 2: Set boundaries correctly

    Top and left start at 0, bottom is last row index (length-1), right is last column index (length-1). int top = 0, bottom = matrix.length - 1, left = 0, right = matrix[0].length - 1; matches this.
  3. Final Answer:

    int top = 0, bottom = matrix.length - 1, left = 0, right = matrix[0].length - 1; -> Option B
  4. Quick Check:

    Boundaries start at 0 and end at length-1 [OK]
Hint: Boundaries start at 0, end at length-1 [OK]
Common Mistakes:
  • Using 1-based indices instead of 0-based
  • Swapping rows and columns in boundaries
  • Setting bottom or right greater than max index
3. What is the output of the following Java code snippet for spiral traversal?
int[][] matrix = {
  {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++) System.out.print(matrix[top][i] + " ");
  top++;
  for (int i = top; i <= bottom; i++) System.out.print(matrix[i][right] + " ");
  right--;
  if (top <= bottom) {
    for (int i = right; i >= left; i--) System.out.print(matrix[bottom][i] + " ");
    bottom--;
  }
  if (left <= right) {
    for (int i = bottom; i >= top; i--) System.out.print(matrix[i][left] + " ");
    left++;
  }
}
medium
A. 1 4 7 8 9 6 3 2 5
B. 1 2 3 4 5 6 7 8 9
C. 1 2 3 6 9 8 7 4 5
D. 9 8 7 6 5 4 3 2 1

Solution

  1. Step 1: Trace first outer layer

    Top row: 1 2 3, right column down: 6 9, bottom row left: 8 7, left column up: 4
  2. Step 2: Trace inner layer

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

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

    Spiral order matches 1 2 3 6 9 8 7 4 5 [OK]
Hint: Follow boundaries and print edges inward [OK]
Common Mistakes:
  • Printing rows or columns in wrong order
  • Missing center element in odd-sized matrix
  • Not updating boundaries correctly
4. Identify the bug in this spiral traversal code snippet:
int top = 0, bottom = matrix.length - 1;
int left = 0, right = matrix[0].length - 1;
while (top < bottom && left < right) {
  for (int i = left; i <= right; i++) System.out.print(matrix[top][i] + " ");
  top++;
  for (int i = top; i <= bottom; i++) System.out.print(matrix[i][right] + " ");
  right--;
  for (int i = right; i >= left; i--) System.out.print(matrix[bottom][i] + " ");
  bottom--;
  for (int i = bottom; i >= top; i--) System.out.print(matrix[i][left] + " ");
  left++;
}
medium
A. The while condition should use <= instead of < to include all layers
B. The for loops should use < instead of <= to avoid index errors
C. The boundaries are initialized incorrectly
D. The print statements should be inside if conditions to avoid duplicates

Solution

  1. Step 1: Analyze while loop condition

    Using < excludes the case when top == bottom or left == right, missing last layer in odd-sized matrices.
  2. Step 2: Correct condition to include all layers

    Changing to <= ensures the center or last layer is processed.
  3. Final Answer:

    The while condition should use <= instead of < to include all layers -> Option A
  4. Quick Check:

    Use <= in while to cover all layers [OK]
Hint: Use <= in while to cover all matrix layers [OK]
Common Mistakes:
  • Using < excludes center element in odd matrices
  • Not updating boundaries after each loop
  • Missing if checks for inner loops
5. Given a rectangular matrix of size 3x4:
int[][] matrix = {
  {1, 2, 3, 4},
  {5, 6, 7, 8},
  {9, 10, 11, 12}
};

What is the spiral traversal output?
hard
A. 1 5 9 10 11 12 8 4 3 2 6 7
B. 1 2 3 4 8 12 11 10 9 5 7 6
C. 1 2 3 4 8 12 11 10 9 6 5 7
D. 1 2 3 4 8 12 11 10 9 5 6 7

Solution

  1. Step 1: Traverse top row left to right

    Print 1 2 3 4
  2. Step 2: Traverse right column top to bottom

    Print 8 12
  3. Step 3: Traverse bottom row right to left

    Print 11 10 9
  4. Step 4: Traverse left column bottom to top

    Print 5
  5. Step 5: Traverse inner row left to right

    Print 6 7
  6. Final Answer:

    1 2 3 4 8 12 11 10 9 5 6 7 -> Option D
  7. Quick Check:

    Follow edges inward for rectangular matrix [OK]
Hint: Print edges clockwise, move inward layer by layer [OK]
Common Mistakes:
  • Mixing order of inner elements
  • Skipping last inner row
  • Confusing left column traversal direction
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