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 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);
Attempts:
2 left
💡 Hint
Follow the boundaries top, right, bottom, and left carefully in order.
✗ Incorrect
The spiral traversal starts from top row left to right, then right column top to bottom, then bottom row right to left, then left column bottom to top, and repeats inward. The output matches option C.
❓ Predict Output
intermediateSpiral 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);
Attempts:
2 left
💡 Hint
Remember to move boundaries inward after each side is processed.
✗ Incorrect
The spiral order is top row left to right, right column top to bottom, bottom row right to left, and left column bottom to top. The output matches option D.
🔧 Debug
advancedIdentify 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);
Attempts:
2 left
💡 Hint
Check the while loop condition carefully.
✗ Incorrect
The while loop uses 'top < bottom && left < right' which skips the case when top == bottom or left == right, causing some elements to be missed in the output.
🧠 Conceptual
advancedNumber 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 spiral layer covers one outer ring, reducing both dimensions by 2. The number of layers is the smaller dimension divided by 2, rounded down.
🚀 Application
expertFinal 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);
Attempts:
2 left
💡 Hint
Count how many times each boundary moves inward during traversal.
✗ Incorrect
Each layer moves top and left up by 1 and bottom and right down by 1. For 4x4, after 2 layers, top=2, bottom=1, left=2, right=1.
