Array Reversal Techniques in DSA Python - Time & Space Complexity
We want to understand how the time needed to reverse an array changes as the array gets bigger.
How does the number of steps grow when we reverse more elements?
Analyze the time complexity of the following code snippet.
def reverse_array(arr):
start = 0
end = len(arr) - 1
while start < end:
arr[start], arr[end] = arr[end], arr[start]
start += 1
end -= 1
This code reverses the elements of an array by swapping pairs from the ends moving towards the center.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Swapping two elements in the array.
- How many times: About half the length of the array (n/2 times).
As the array size grows, the number of swaps grows roughly half as fast.
| Input Size (n) | Approx. Operations (Swaps) |
|---|---|
| 10 | 5 |
| 100 | 50 |
| 1000 | 500 |
Pattern observation: The operations grow linearly with input size, just half as many swaps as elements.
Time Complexity: O(n)
This means the time to reverse the array grows directly in proportion to the number of elements.
[X] Wrong: "Reversing an array takes constant time because we just swap pairs."
[OK] Correct: Even though each swap is quick, the number of swaps grows with the array size, so total time grows too.
Understanding how array reversal scales helps you reason about loops and data manipulation, a key skill in many coding challenges.
"What if we used recursion to reverse the array instead of a loop? How would the time complexity change?"
Practice
Solution
Step 1: Understand the meaning of reversal
Reversing means flipping the order of elements so the last element comes first and the first element comes last.Step 2: Compare with other options
Sorting arranges elements by value, removing duplicates deletes repeats, and adding elements is insertion, not reversal.Final Answer:
Changing the order of elements so the last becomes first and first becomes last -> Option BQuick Check:
Reversal = Flip order [OK]
- Confusing reversal with sorting
- Thinking reversal removes duplicates
- Mixing reversal with adding elements
arr in-place?Solution
Step 1: Identify the in-place reversal method
In Python,arr.reverse()reverses the list in-place and returns None.Step 2: Check other options
reverse(arr)is not a valid function,arr = arr.reverse()assigns None to arr, andarr[::-1]creates a reversed copy but does not change arr in-place.Final Answer:
arr.reverse() -> Option CQuick Check:
In-place reverse uses arr.reverse() [OK]
- Assigning arr = arr.reverse() which sets arr to None
- Using slicing without assignment for in-place change
- Calling a non-existent reverse() function
arr = [1, 2, 3, 4, 5] reversed_arr = arr[::-1] print(reversed_arr) print(arr)
Solution
Step 1: Understand slicing with negative step
The slicearr[::-1]creates a new list with elements in reverse order.Step 2: Check original list after slicing
The original listarrremains unchanged because slicing returns a new list.Final Answer:
[5, 4, 3, 2, 1] and [1, 2, 3, 4, 5] -> Option AQuick Check:
Slicing reverses copy, original unchanged [OK]
- Thinking slicing changes original list
- Expecting error from slicing
- Confusing reversed copy with in-place change
arr in-place:def reverse_list(arr):
for i in range(len(arr)):
arr[i], arr[len(arr)-i] = arr[len(arr)-i], arr[i]
arr = [1, 2, 3, 4]
reverse_list(arr)
print(arr)Solution
Step 1: Analyze index usage in swapping
The code usesarr[len(arr)-i]which causes index out of range because list indices go from 0 to len(arr)-1.Step 2: Identify correct index for swapping
The correct index should belen(arr)-1 - ito avoid going beyond the last index.Final Answer:
Index out of range error due to incorrect index calculation -> Option DQuick Check:
Index error from len(arr)-i used as index [OK]
- Using len(arr)-i instead of len(arr)-1 - i
- Not realizing list indices start at 0
- Assuming function returns reversed list
arr = [10, 20, 30, 40, 50]. Write a function that reverses the list without using built-in reverse methods or slicing. Which approach below correctly reverses the list in-place?Solution
Step 1: Understand in-place reversal without built-ins
Swapping elements from start and end moving towards the center changes the list in-place without extra space.Step 2: Evaluate other options
Creating a new list is not in-place, sorting changes order by value, and assigning without swapping overwrites data incorrectly.Final Answer:
Swap elements from start and end moving towards center using a while loop -> Option AQuick Check:
In-place reversal = swap ends towards center [OK]
- Using sorting instead of reversal
- Overwriting elements without swapping
- Creating new list instead of in-place change
Ready to prove it?
Pass the test and this pattern moves unknown
