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 Traversal
What is the output of the following code that traverses a 2D array row-wise?
DSA Python
arr = [[1, 2], [3, 4]] result = [] for row in arr: for val in row: result.append(val) print(result)
Attempts:
2 left
💡 Hint
Think about how nested loops go through each element in order.
✗ Incorrect
The outer loop goes row by row, and the inner loop goes through each element in the row, so the elements are added in order: 1, 2, 3, 4.
❓ Predict Output
intermediate2:00remaining
Output of Reverse Array Traversal
What is the output of this code that traverses an array backwards?
DSA Python
arr = [10, 20, 30, 40] result = [] for i in range(len(arr)-1, -1, -1): result.append(arr[i]) print(result)
Attempts:
2 left
💡 Hint
The loop starts from the last index and goes to the first.
✗ Incorrect
The loop goes from the last element to the first, so the result is the reversed array.
🔧 Debug
advanced2:00remaining
Identify the Error in Diagonal Traversal
What error does this code produce when trying to traverse the main diagonal of a square matrix?
DSA Python
matrix = [[1,2,3],[4,5,6],[7,8,9]] for i in range(len(matrix)): print(matrix[i][i+1])
Attempts:
2 left
💡 Hint
Check the index used inside the loop for accessing elements.
✗ Incorrect
The code tries to access matrix[i][i+1], which goes out of range when i is at the last index.
🧠 Conceptual
advanced2:00remaining
Number of Elements Visited in Zigzag Traversal
Given a 3x3 matrix, how many elements are visited if we traverse it in a zigzag pattern row-wise (left to right on first row, right to left on second, and so on)?
Attempts:
2 left
💡 Hint
Zigzag means visiting every element but changing direction each row.
✗ Incorrect
All elements are visited once, just the order changes, so total is 9.
🚀 Application
expert3:00remaining
Output After Spiral Traversal of 3x3 Matrix
What is the output list after performing a spiral traversal on this 3x3 matrix?
matrix = [[1,2,3],[4,5,6],[7,8,9]]
DSA Python
def spiral_traversal(matrix): result = [] top, bottom = 0, len(matrix) - 1 left, right = 0, len(matrix[0]) - 1 while left <= right and top <= bottom: for i in range(left, right + 1): result.append(matrix[top][i]) top += 1 for i in range(top, bottom + 1): result.append(matrix[i][right]) right -= 1 if top <= bottom: for i in range(right, left - 1, -1): result.append(matrix[bottom][i]) bottom -= 1 if left <= right: for i in range(bottom, top - 1, -1): result.append(matrix[i][left]) left += 1 return result print(spiral_traversal(matrix))
Attempts:
2 left
💡 Hint
Spiral traversal goes around the edges inward in a clockwise direction.
✗ Incorrect
The spiral order visits top row left to right, right column top to bottom, bottom row right to left, left column bottom to top, then repeats inward.