Challenge - 5 Problems
Array Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Nested Loop Array Traversal
What is the printed output of the following C code snippet that traverses a 2D array?
DSA C
int arr[2][3] = {{1, 2, 3}, {4, 5, 6}}; for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { printf("%d ", arr[i][j]); } printf("\n"); }
Attempts:
2 left
💡 Hint
Think about how the outer loop controls rows and the inner loop controls columns.
✗ Incorrect
The outer loop runs twice for each row, and the inner loop prints all columns in that row before moving to the next line.
❓ Predict Output
intermediate2:00remaining
Output of Single Loop Traversal with Step
What is the output of this C code that traverses an array with a step of 2?
DSA C
int arr[] = {10, 20, 30, 40, 50, 60}; int n = 6; for (int i = 0; i < n; i += 2) { printf("%d ", arr[i]); }
Attempts:
2 left
💡 Hint
The loop increments by 2, so it skips every other element.
✗ Incorrect
Starting at index 0, the loop prints elements at indices 0, 2, and 4.
🔧 Debug
advanced2:00remaining
Identify the Error in Reverse Array Traversal
What error does this C code produce when trying to print an array in reverse?
DSA C
int arr[] = {1, 2, 3, 4, 5}; int n = 5; for (int i = n - 1; i >= 0; i--) { printf("%d ", arr[i]); }
Attempts:
2 left
💡 Hint
Check the starting index and valid array indices.
✗ Incorrect
Array indices go from 0 to n-1. Starting at n accesses arr[5], which is out of bounds causing runtime error.
🧠 Conceptual
advanced2:00remaining
Understanding Traversal Order in Zigzag Pattern
Given a 2D array, which traversal pattern prints elements in a zigzag order (left to right on even rows, right to left on odd rows)?
DSA C
int arr[3][3] = {{1,2,3},{4,5,6},{7,8,9}}; for (int i = 0; i < 3; i++) { if (i % 2 == 0) { for (int j = 0; j < 3; j++) { printf("%d ", arr[i][j]); } } else { for (int j = 2; j >= 0; j--) { printf("%d ", arr[i][j]); } } printf("\n"); }
Attempts:
2 left
💡 Hint
Even rows go left to right, odd rows go right to left.
✗ Incorrect
The code prints row 0 left to right, row 1 right to left, row 2 left to right, creating a zigzag pattern.
🚀 Application
expert3:00remaining
Count Unique Elements Using Array Traversal
Given an unsorted integer array, which code snippet correctly counts the number of unique elements using only array traversal and no extra data structures?
DSA C
int arr[] = {2, 3, 2, 4, 3, 5}; int n = 6; int count = 0; for (int i = 0; i < n; i++) { int j; for (j = 0; j < i; j++) { if (arr[i] == arr[j]) { break; } } if (j == i) { count++; } } printf("%d", count);
Attempts:
2 left
💡 Hint
Count increments only when current element is not found before in the array.
✗ Incorrect
The code checks if the current element appeared before; if not, it counts it as unique. The unique elements are 2, 3, 4, 5 totaling 4.
