0
0
DSA Pythonprogramming~20 mins

Array Traversal Patterns in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Array Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A[[1, 2], [3, 4]]
B[1, 3, 2, 4]
C[1, 2, 3, 4]
D[4, 3, 2, 1]
Attempts:
2 left
💡 Hint
Think about how nested loops go through each element in order.
Predict Output
intermediate
2: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)
A[40, 30, 20, 10]
B[10, 20, 30, 40]
C[40, 20, 30, 10]
D[30, 40, 20, 10]
Attempts:
2 left
💡 Hint
The loop starts from the last index and goes to the first.
🔧 Debug
advanced
2: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])
ANo error, prints diagonal elements
BTypeError: unsupported operand type(s)
CSyntaxError: invalid syntax
DIndexError: list index out of range
Attempts:
2 left
💡 Hint
Check the index used inside the loop for accessing elements.
🧠 Conceptual
advanced
2: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)?
A9
B6
C3
D12
Attempts:
2 left
💡 Hint
Zigzag means visiting every element but changing direction each row.
🚀 Application
expert
3: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))
A[9, 8, 7, 6, 5, 4, 3, 2, 1]
B[1, 2, 3, 6, 9, 8, 7, 4, 5]
C[1, 2, 3, 4, 5, 6, 7, 8, 9]
D[1, 4, 7, 8, 9, 6, 3, 2, 5]
Attempts:
2 left
💡 Hint
Spiral traversal goes around the edges inward in a clockwise direction.