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
What is the output of the following C# code that traverses a 2D array using nested loops?
DSA C#
int[,] matrix = { {1, 2}, {3, 4} }; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { Console.Write(matrix[i, j] + " "); } }
Attempts:
2 left
💡 Hint
Remember that the outer loop controls rows and the inner loop controls columns.
✗ Incorrect
The outer loop iterates over rows (i), and the inner loop iterates over columns (j). So the elements are printed row-wise: 1 2 3 4.
❓ Predict Output
intermediateOutput of Reverse Array Traversal
What is the output of this C# code that traverses an array in reverse order?
DSA C#
int[] arr = {5, 10, 15, 20}; for (int i = arr.Length - 1; i >= 0; i--) { Console.Write(arr[i] + " "); }
Attempts:
2 left
💡 Hint
The loop starts from the last index and moves backward.
✗ Incorrect
The loop prints elements starting from the last index to the first, so output is 20 15 10 5.
🧠 Conceptual
advancedUnderstanding Zigzag Traversal Pattern
In a zigzag traversal of a 2D array, rows are traversed alternately from left to right and right to left. Given the 2D array below, what is the output of zigzag traversal?
DSA C#
int[,] matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; // Zigzag traversal code not shown
Attempts:
2 left
💡 Hint
Odd rows are traversed right to left, even rows left to right.
✗ Incorrect
Row 0 left to right: 1 2 3; Row 1 right to left: 6 5 4; Row 2 left to right: 7 8 9. So output is 1 2 3 6 5 4 7 8 9 (option C).
🔧 Debug
advancedIdentify the Error in Diagonal Traversal Code
What error will this C# code produce when trying to print the main diagonal of a 2D square array?
DSA C#
int[,] matrix = { {1, 2}, {3, 4} }; for (int i = 0; i < matrix.GetLength(0); i++) { Console.Write(matrix[i, i] + " "); }
Attempts:
2 left
💡 Hint
Check the loop boundary condition carefully.
✗ Incorrect
matrix.GetLength(0) returns 2, valid indices are 0 and 1. Loop runs i=0 to 1 inclusive, so it prints 1 and 4 without error.
🚀 Application
expertCount Unique Elements Using Array Traversal
Given an integer array, which code snippet correctly counts the number of unique elements using array traversal?
Attempts:
2 left
💡 Hint
Check which code counts unique elements correctly without errors.
✗ Incorrect
Option B correctly checks each element against previous elements to count unique ones. Option B counts duplicates, option B causes IndexOutOfRangeException, option B uses HashSet which is not allowed as per traversal-only constraint.
