Challenge - 5 Problems
Array Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of nested loop traversal on 2D array
What is the output of the following Java code that traverses a 2D array row-wise and prints elements?
DSA Java
int[][] arr = {{1, 2}, {3, 4}, {5, 6}}; for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[i].length; j++) { System.out.print(arr[i][j] + " "); } }
Attempts:
2 left
💡 Hint
Think about how the outer loop goes through rows and inner loop goes through columns.
✗ Incorrect
The outer loop goes through each row, and the inner loop prints each element in that row from left to right, so the output is all elements in order: 1 2 3 4 5 6.
❓ Predict Output
intermediateOutput of reverse traversal on 1D array
What is the output of this Java code that traverses an array backwards and prints elements?
DSA Java
int[] arr = {10, 20, 30, 40}; for (int i = arr.length - 1; i >= 0; i--) { System.out.print(arr[i] + " "); }
Attempts:
2 left
💡 Hint
The loop starts from the last index and moves to the first.
✗ Incorrect
The loop prints elements starting from the last index to the first, so the output is 40 30 20 10.
🔧 Debug
advancedIdentify the error in diagonal traversal of square matrix
What error will this Java code produce when trying to print the main diagonal of a 3x3 matrix?
DSA Java
int[][] matrix = {{1,2,3},{4,5,6},{7,8,9}}; for (int i = 0; i < matrix.length; i++) { System.out.print(matrix[i][i] + " "); }
Attempts:
2 left
💡 Hint
Check the loop condition and array indexing carefully.
✗ Incorrect
The loop runs from i = 0 to i = matrix.length (which is 3), but valid indices are 0 to 2. Accessing matrix[3][3] causes ArrayIndexOutOfBoundsException.
🧠 Conceptual
advancedNumber of elements visited in zigzag traversal of 2D array
Consider a 3x3 2D array. If you traverse it in a zigzag pattern starting from top-left, moving right, then diagonally down-left, then right again, and so on until all elements are visited, how many elements will be visited?
Attempts:
2 left
💡 Hint
Think about how many elements are in the array and if any are skipped.
✗ Incorrect
A 3x3 array has 9 elements. Zigzag traversal visits each element exactly once, so total visited elements are 9.
🚀 Application
expertOutput after alternating forward and backward traversal on array
Given the Java code below, what is the printed output?
DSA Java
int[] arr = {1, 2, 3, 4, 5}; for (int i = 0; i < arr.length; i++) { if (i % 2 == 0) { System.out.print(arr[i] + " "); } else { System.out.print(arr[arr.length - 1 - i] + " "); } }
Attempts:
2 left
💡 Hint
Check how the index changes for even and odd i values.
✗ Incorrect
For i=0 (even), print arr[0]=1; i=1 (odd), print arr[3]=4; i=2 (even), print arr[2]=3; i=3 (odd), print arr[1]=2; i=4 (even), print arr[4]=5. Output: 1 4 3 2 5.
