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 array traversal with condition
What is the output of the following JavaScript code that traverses a 2D array and collects values greater than 3?
DSA Javascript
const arr = [[1, 4], [3, 5], [2, 6]]; const result = []; for (let i = 0; i < arr.length; i++) { for (let j = 0; j < arr[i].length; j++) { if (arr[i][j] > 3) { result.push(arr[i][j]); } } } console.log(result);
Attempts:
2 left
💡 Hint
Focus on the condition inside the nested loops and which values satisfy it.
✗ Incorrect
The code checks each element in the 2D array and adds it to the result only if it is greater than 3. Values 4, 5, and 6 satisfy this condition.
❓ Predict Output
intermediateOutput of reverse traversal with step
What is the output of this code that traverses an array backwards, skipping every other element?
DSA Javascript
const arr = [10, 20, 30, 40, 50, 60]; const result = []; for (let i = arr.length - 1; i >= 0; i -= 2) { result.push(arr[i]); } console.log(result);
Attempts:
2 left
💡 Hint
Look at the loop decrement and how it skips elements.
✗ Incorrect
The loop starts at index 5 (value 60) and decreases by 2 each time, so it picks elements at indices 5, 3, and 1 which are 60, 40, and 20.
🔧 Debug
advancedIdentify the error in array traversal with forEach
What error will this code produce when trying to traverse and modify an array using forEach?
DSA Javascript
const arr = [1, 2, 3]; arr.forEach((num, index) => { if (num === 2) { arr.splice(index, 1); } }); console.log(arr);
Attempts:
2 left
💡 Hint
Consider what happens when you modify an array while looping with forEach.
✗ Incorrect
Modifying the array during forEach traversal can cause unexpected behavior because the internal index tracking gets confused, but no error is thrown.
❓ Predict Output
advancedOutput of map with nested arrays and flattening
What is the output of this code that maps and flattens a nested array?
DSA Javascript
const arr = [[1, 2], [3, 4], [5]]; const result = arr.map(subArr => subArr.map(x => x * 2)).flat(); console.log(result);
Attempts:
2 left
💡 Hint
Remember that map returns arrays and flat removes one level of nesting.
✗ Incorrect
Each inner array is doubled element-wise, then flat removes one level of nesting, resulting in a single array with doubled values.
🧠 Conceptual
expertTime complexity of nested array traversal with early break
Consider a nested loop traversing a 2D array of size n x n. The inner loop breaks early when a condition is met. What is the best case time complexity of this traversal?
Attempts:
2 left
💡 Hint
Think about how early breaks affect the number of inner loop iterations.
✗ Incorrect
If the inner loop breaks immediately for each outer iteration, the total operations are proportional to n, so best case is O(n).
