Bird
Raised Fist0
DSA Pythonprogramming~20 mins

Array Traversal Patterns in DSA Python - Practice Problems & Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What does the following code do?
for i in range(len(arr)):
  print(arr[i])

Assuming arr is a list of numbers.
easy
A. Prints only the first element of the array.
B. Prints all elements of the array from first to last.
C. Prints the indices of the array elements.
D. Prints the array in reverse order.

Solution

  1. Step 1: Understand the loop range

    The loop runs from 0 to len(arr)-1, covering all valid indices of the array.
  2. Step 2: Access and print each element

    Inside the loop, arr[i] accesses each element in order, printing them one by one.
  3. Final Answer:

    Prints all elements of the array from first to last. -> Option B
  4. Quick Check:

    Loop over indices prints elements in order [OK]
Hint: Loop over range(len(arr)) accesses all elements by index [OK]
Common Mistakes:
  • Thinking it prints indices instead of elements
  • Assuming it prints in reverse order
  • Confusing range(len(arr)) with just range(arr)
2. Which of the following is the correct syntax to traverse all elements in a list arr using a for-each style loop in Python?
easy
A. for i to len(arr): print(arr[i])
B. for i in range(arr): print(i)
C. for element in arr: print(element)
D. for element in range(len(arr)): print(element)

Solution

  1. Step 1: Identify correct for-each syntax

    Python uses for variable in iterable to loop over elements directly.
  2. Step 2: Check each option

    for element in arr: print(element) uses for element in arr, which correctly loops over elements. Others have syntax errors or misuse range.
  3. Final Answer:

    for element in arr: print(element) -> Option C
  4. Quick Check:

    For-each loop uses 'for element in arr' [OK]
Hint: Use 'for element in arr' to loop elements directly [OK]
Common Mistakes:
  • Using range(arr) instead of range(len(arr))
  • Writing 'for i to len(arr)' which is invalid syntax
  • Confusing indices with elements in for-each loops
3. What is the output of the following code?
arr = [10, 20, 30, 40]
for i in range(1, len(arr)):
  print(arr[i])
medium
A. 20 30 40
B. 40
C. 10 20 30
D. 10 20 30 40

Solution

  1. Step 1: Analyze the loop range

    The loop runs from 1 to len(arr)-1, which is 1 to 3 for this array.
  2. Step 2: Print elements at indices 1, 2, 3

    Elements are arr[1]=20, arr[2]=30, arr[3]=40, printed in order.
  3. Final Answer:

    20 30 40 -> Option A
  4. Quick Check:

    Range(1,4) prints elements from index 1 to 3 [OK]
Hint: range(1, len(arr)) skips first element [OK]
Common Mistakes:
  • Including the first element (index 0) mistakenly
  • Confusing range end as inclusive
  • Printing indices instead of elements
4. Find the error in this code snippet that tries to print all elements of arr:
arr = [1, 2, 3, 4]
for i in range(len(arr)):
  print(arr[i+1])
medium
A. No error, prints all elements correctly.
B. SyntaxError due to incorrect for loop syntax.
C. Prints elements in reverse order.
D. IndexError because i+1 goes out of range on last iteration.

Solution

  1. Step 1: Check loop indices and access

    Loop runs i from 0 to 3 (len(arr)=4). Accessing arr[i+1] means indices 1 to 4.
  2. Step 2: Identify out of range access

    When i=3, arr[4] is accessed, which is outside the array bounds causing IndexError.
  3. Final Answer:

    IndexError because i+1 goes out of range on last iteration. -> Option D
  4. Quick Check:

    Accessing arr[i+1] with i at last index causes error [OK]
Hint: Check index +1 can exceed array length [OK]
Common Mistakes:
  • Ignoring index out of range error
  • Thinking syntax is wrong instead
  • Assuming it prints reversed elements
5. Given a list arr = [3, 5, 7, 9, 11], write code to print elements at even indices only using array traversal patterns. Which code snippet does this correctly?
hard
A. for i in range(0, len(arr), 2): print(arr[i])
B. for i in range(1, len(arr), 2): print(arr[i])
C. for element in arr: if element % 2 == 0: print(element)
D. for i in range(len(arr)): if i % 2 == 1: print(arr[i])

Solution

  1. Step 1: Understand even index traversal

    Even indices are 0, 2, 4 for this list. Step size 2 starting at 0 covers these.
  2. Step 2: Check each option

    for i in range(0, len(arr), 2): print(arr[i]) uses range(0, len(arr), 2) which correctly iterates over even indices. Others either use odd indices or check element values incorrectly.
  3. Final Answer:

    for i in range(0, len(arr), 2): print(arr[i]) -> Option A
  4. Quick Check:

    Step 2 in range picks even indices [OK]
Hint: Use range(start=0, end, step=2) for even indices [OK]
Common Mistakes:
  • Using odd indices instead of even
  • Checking element value parity instead of index
  • Forgetting step argument in range
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard