Array Traversal Patterns in DSA Python - Time & Space Complexity
When we look at how we go through an array, we want to know how long it takes as the array gets bigger.
We ask: How does the work grow when the array size grows?
Analyze the time complexity of the following code snippet.
def print_elements(arr):
for element in arr:
print(element)
This code goes through each item in the array and prints it once.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each element in the array.
- How many times: Exactly once for each element, so as many times as the array length.
As the array gets bigger, the number of print actions grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 prints |
| 100 | 100 prints |
| 1000 | 1000 prints |
Pattern observation: The work grows directly with the size of the array.
Time Complexity: O(n)
This means if the array doubles in size, the time to go through it also doubles.
[X] Wrong: "Since we only have one loop, the time is constant no matter the array size."
[OK] Correct: Even one loop runs once per element, so more elements mean more work, not the same.
Understanding how simple loops grow with input size is a key skill that helps you analyze more complex code easily.
"What if we nested another loop inside to compare every element with every other? How would the time complexity change?"
Practice
for i in range(len(arr)):
print(arr[i])Assuming
arr is a list of numbers.Solution
Step 1: Understand the loop range
The loop runs from 0 to len(arr)-1, covering all valid indices of the array.Step 2: Access and print each element
Inside the loop,arr[i]accesses each element in order, printing them one by one.Final Answer:
Prints all elements of the array from first to last. -> Option BQuick Check:
Loop over indices prints elements in order [OK]
- Thinking it prints indices instead of elements
- Assuming it prints in reverse order
- Confusing range(len(arr)) with just range(arr)
arr using a for-each style loop in Python?Solution
Step 1: Identify correct for-each syntax
Python usesfor variable in iterableto loop over elements directly.Step 2: Check each option
for element in arr: print(element) usesfor element in arr, which correctly loops over elements. Others have syntax errors or misuse range.Final Answer:
for element in arr: print(element) -> Option CQuick Check:
For-each loop uses 'for element in arr' [OK]
- 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
arr = [10, 20, 30, 40]
for i in range(1, len(arr)):
print(arr[i])
Solution
Step 1: Analyze the loop range
The loop runs from 1 to len(arr)-1, which is 1 to 3 for this array.Step 2: Print elements at indices 1, 2, 3
Elements are arr[1]=20, arr[2]=30, arr[3]=40, printed in order.Final Answer:
20 30 40 -> Option AQuick Check:
Range(1,4) prints elements from index 1 to 3 [OK]
- Including the first element (index 0) mistakenly
- Confusing range end as inclusive
- Printing indices instead of elements
arr:arr = [1, 2, 3, 4]
for i in range(len(arr)):
print(arr[i+1])
Solution
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.Step 2: Identify out of range access
When i=3, arr[4] is accessed, which is outside the array bounds causing IndexError.Final Answer:
IndexError because i+1 goes out of range on last iteration. -> Option DQuick Check:
Accessing arr[i+1] with i at last index causes error [OK]
- Ignoring index out of range error
- Thinking syntax is wrong instead
- Assuming it prints reversed elements
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?Solution
Step 1: Understand even index traversal
Even indices are 0, 2, 4 for this list. Step size 2 starting at 0 covers these.Step 2: Check each option
for i in range(0, len(arr), 2): print(arr[i]) usesrange(0, len(arr), 2)which correctly iterates over even indices. Others either use odd indices or check element values incorrectly.Final Answer:
for i in range(0, len(arr), 2): print(arr[i]) -> Option AQuick Check:
Step 2 in range picks even indices [OK]
- Using odd indices instead of even
- Checking element value parity instead of index
- Forgetting step argument in range
Ready to prove it?
Pass the test and this pattern moves unknown
