Challenge - 5 Problems
Array Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Zigzag Array Traversal
What is the output of the following C++ code that traverses a 2D array in a zigzag pattern?
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++) { std::cout << arr[i][j] << " "; } } else { for(int j=2; j>=0; j--) { std::cout << arr[i][j] << " "; } } }
Attempts:
2 left
💡 Hint
Look at how the inner loop changes direction based on the row index.
✗ Incorrect
The code prints rows in normal order for even rows and reversed order for odd rows, creating a zigzag pattern.
❓ Predict Output
intermediateOutput of Spiral Traversal of 3x3 Matrix
What is the output of this C++ code that prints elements of a 3x3 matrix in spiral order?
DSA C++
int matrix[3][3] = {{1,2,3},{4,5,6},{7,8,9}}; int top=0, bottom=2, left=0, right=2; while(top <= bottom && left <= right) { for(int i=left; i<=right; i++) std::cout << matrix[top][i] << " "; top++; for(int i=top; i<=bottom; i++) std::cout << matrix[i][right] << " "; right--; if(top <= bottom) { for(int i=right; i>=left; i--) std::cout << matrix[bottom][i] << " "; bottom--; } if(left <= right) { for(int i=bottom; i>=top; i--) std::cout << matrix[i][left] << " "; left++; } }
Attempts:
2 left
💡 Hint
Follow the boundaries top, bottom, left, right as they shrink inward.
✗ Incorrect
The code prints the outer layer clockwise, then moves inward, producing a spiral order.
🧠 Conceptual
advancedUnderstanding Time Complexity of Nested Array Traversal
If you have a nested loop traversing a 2D array of size n x n, what is the time complexity of the traversal?
Attempts:
2 left
💡 Hint
Think about how many elements are visited in total.
✗ Incorrect
Traversing each element in an n x n matrix requires visiting n*n elements, so the time complexity is O(n^2).
🔧 Debug
advancedIdentify the Bug in Reverse Array Traversal Code
What error will this C++ code produce when trying to print an array in reverse?
DSA C++
int arr[5] = {10, 20, 30, 40, 50}; for(int i=4; i>=0; i--) { std::cout << arr[i] << " "; }
Attempts:
2 left
💡 Hint
Check the starting index and valid array indices.
✗ Incorrect
The loop starts at i=5 but valid indices are 0 to 4, so arr[5] is out of bounds causing runtime error.
🚀 Application
expertFind the Missing Number Using Array Traversal
Given an array of size n containing numbers from 1 to n+1 with one missing number, which approach correctly finds the missing number using traversal?
Attempts:
2 left
💡 Hint
Think about a simple arithmetic property that helps find the missing number.
✗ Incorrect
Summing all elements and subtracting from the expected total sum is efficient and uses simple traversal.
