Bird
Raised Fist0
DSA Javaprogramming~20 mins

Array Traversal Patterns in DSA Java - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Array Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output 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] + " ");
    }
}
A1 3 5 2 4 6
B2 1 4 3 6 5
C6 5 4 3 2 1
D1 2 3 4 5 6
Attempts:
2 left
💡 Hint
Think about how the outer loop goes through rows and inner loop goes through columns.
❓ Predict Output
intermediate
2:00remaining
Output 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] + " ");
}
A40 20 30 10
B10 20 30 40
C40 30 20 10
D30 40 20 10
Attempts:
2 left
💡 Hint
The loop starts from the last index and moves to the first.
🔧 Debug
advanced
2:00remaining
Identify 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] + " ");
}
AArrayIndexOutOfBoundsException
BNullPointerException
CSyntaxError
DPrints: 1 5 9
Attempts:
2 left
💡 Hint
Check the loop condition and array indexing carefully.
🧠 Conceptual
advanced
2:00remaining
Number 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?
A8
B9
C6
D12
Attempts:
2 left
💡 Hint
Think about how many elements are in the array and if any are skipped.
🚀 Application
expert
3:00remaining
Output 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] + " ");
    }
}
A1 4 3 2 5
B1 3 3 3 5
C1 3 3 2 5
D1 4 3 4 5
Attempts:
2 left
💡 Hint
Check how the index changes for even and odd i values.

Practice

(1/5)
1. What does the following Java code snippet do?
for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); }
easy
A. Prints all elements of the array from end to start
B. Prints only the first element of the array
C. Prints all elements of the array from start to end
D. Prints only the last element of the array

Solution

  1. Step 1: Understand the loop range

    The loop starts at index 0 and runs while i is less than the array length, so it covers all valid indices from start to end.
  2. Step 2: Analyze the print statement inside the loop

    Each element at index i is printed followed by a space, so all elements are printed in order.
  3. Final Answer:

    Prints all elements of the array from start to end -> Option C
  4. Quick Check:

    Loop from 0 to length-1 prints all elements [OK]
Hint: Loop from 0 to length-1 prints all elements in order [OK]
Common Mistakes:
  • Confusing loop direction and thinking it prints backwards
  • Assuming it prints only one element
  • Ignoring the loop condition
2. Which of the following is the correct syntax to traverse an array arr backwards in Java?
easy
A. for (int i = arr.length; i > 0; i--)
B. for (int i = 0; i < arr.length; i++)
C. for (int i = 1; i <= arr.length; i++)
D. for (int i = arr.length - 1; i >= 0; i--)

Solution

  1. Step 1: Identify correct start and end indices for backward traversal

    Backward traversal starts at last index (arr.length - 1) and ends at 0 inclusive.
  2. Step 2: Check loop condition and decrement

    The loop continues while i is greater than or equal to 0, decrementing i each time, correctly covering all indices backwards.
  3. Final Answer:

    for (int i = arr.length - 1; i >= 0; i--) -> Option D
  4. Quick Check:

    Start at last index, go down to 0 [OK]
Hint: Start at length-1, loop down to 0 with i-- [OK]
Common Mistakes:
  • Starting loop at arr.length (out of bounds)
  • Using i > 0 instead of i >= 0 (misses index 0)
  • Starting loop at 1 instead of length-1
3. What is the output of the following code?
int[] arr = {2, 4, 6, 8};
for (int i = 1; i < arr.length; i += 2) {
    System.out.print(arr[i] + " ");
}
medium
A. 4 8
B. 2 6
C. 4 6 8
D. 2 4 6 8

Solution

  1. Step 1: Identify loop indices

    The loop starts at i=1 and increments by 2, so i takes values 1 and 3 (since arr.length=4).
  2. Step 2: Print elements at indices 1 and 3

    arr[1] = 4 and arr[3] = 8, so output is "4 8 "
  3. Final Answer:

    4 8 -> Option A
  4. Quick Check:

    Indices 1 and 3 printed: 4 and 8 [OK]
Hint: Loop i=1; i<length; i+=2 prints odd indices [OK]
Common Mistakes:
  • Assuming loop prints all elements
  • Confusing index increments
  • Printing elements at even indices instead
4. Find the error in this code snippet that tries to print array elements backwards:
int[] arr = {1, 3, 5, 7};
for (int i = arr.length; i >= 0; i--) {
    System.out.print(arr[i] + " ");
}
medium
A. Loop starts at arr.length which is out of bounds
B. Loop condition should be i <= arr.length
C. Array index should start at 0
D. No error, code works fine

Solution

  1. Step 1: Check starting index of loop

    arr.length is 4, but valid indices are 0 to 3. Starting at 4 causes ArrayIndexOutOfBoundsException.
  2. Step 2: Correct starting index for backward traversal

    Loop should start at arr.length - 1 (3) to access last element safely.
  3. Final Answer:

    Loop starts at arr.length which is out of bounds -> Option A
  4. Quick Check:

    Array indices go from 0 to length-1 [OK]
Hint: Array indices max is length-1, not length [OK]
Common Mistakes:
  • Starting loop at arr.length instead of arr.length-1
  • Ignoring index out of bounds error
  • Confusing loop condition signs
5. Given an integer array arr, which code snippet correctly prints all elements at even indices in reverse order?
hard
A. for (int i = arr.length - 1; i >= 0; i -= 2) { System.out.print(arr[i] + " "); }
B. for (int i = (arr.length % 2 == 0 ? arr.length - 2 : arr.length - 1); i >= 0; i -= 2) { System.out.print(arr[i] + " "); }
C. for (int i = 0; i < arr.length; i += 2) { System.out.print(arr[i] + " "); }
D. for (int i = 1; i < arr.length; i += 2) { System.out.print(arr[i] + " "); }

Solution

  1. Step 1: Determine the highest even index to start from

    If array length is even, highest even index is length-2; if odd, it's length-1.
  2. Step 2: Loop backwards by 2 steps to cover even indices

    Start from the highest even index and decrement by 2 to print all even indices in reverse order.
  3. Final Answer:

    for (int i = (arr.length % 2 == 0 ? arr.length - 2 : arr.length - 1); i >= 0; i -= 2) { System.out.print(arr[i] + " "); } -> Option B
  4. Quick Check:

    Start at highest even index, step down by 2 [OK]
Hint: Start at highest even index, decrement by 2 [OK]
Common Mistakes:
  • Starting at length-1 without checking even index
  • Using i -= 1 instead of i -= 2
  • Printing odd indices instead of even
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