Array Reversal Techniques in DSA Typescript - 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 items?
Analyze the time complexity of the following code snippet.
function reverseArray(arr: number[]): number[] {
let left = 0;
let right = arr.length - 1;
while (left < right) {
const temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}
return arr;
}
This code reverses the elements of an array by swapping pairs from the ends moving toward the center.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Swapping two elements inside a while loop.
- How many times: About half the length of the array (n/2 times).
As the array gets bigger, the number of swaps grows roughly in direct proportion to the size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 5 swaps |
| 100 | 50 swaps |
| 1000 | 500 swaps |
Pattern observation: Doubling the array size roughly doubles the number of swaps needed.
Time Complexity: O(n)
This means the time to reverse grows in a straight line with the number of items in the array.
[X] Wrong: "Reversing an array takes the same time no matter how big it is."
[OK] Correct: The code must swap pairs for each element, so bigger arrays need more swaps and more time.
Knowing how array reversal scales helps you explain your code clearly and choose the best approach in real tasks.
"What if we used recursion to reverse the array instead of a loop? How would the time complexity change?"
Practice
reverse() method do to an array in TypeScript?Solution
Step 1: Understand the
Thereverse()methodreverse()method changes the order of elements in the array so the last element becomes first and the first becomes last.Step 2: Confirm the effect on the original array
This method modifies the original array itself, not creating a new one.Final Answer:
It reverses the order of elements in the original array. -> Option AQuick Check:
reverse()reverses array order [OK]
- Thinking reverse() sorts the array
- Assuming reverse() creates a new array
- Confusing reverse() with removing duplicates
arr in TypeScript?Solution
Step 1: Identify the method call on the array
In TypeScript,reverse()is a method called directly on the array object using dot notation.Step 2: Verify syntax
arr.reverse();is correct.reverse(arr);incorrectly treatsreverseas a standalone function.arr = reverse();misuses the method.arr->reverse();uses invalid syntax.Final Answer:
arr.reverse(); -> Option DQuick Check:
Use dot notation: arr.reverse() [OK]
- Calling reverse() as a standalone function
- Assigning reverse() without calling on array
- Using incorrect symbols like '->'
const arr = [1, 2, 3, 4]; arr.reverse(); console.log(arr);
Solution
Step 1: Understand the initial array
The array starts as [1, 2, 3, 4].Step 2: Apply the reverse() method
Callingarr.reverse()reverses the array in place, changing it to [4, 3, 2, 1].Final Answer:
[4, 3, 2, 1] -> Option AQuick Check:
reverse() flips array order [OK]
- Expecting original order after reverse()
- Confusing reverse() with sorting
- Thinking reverse() returns a new array
const numbers = [5, 6, 7]; const reversed = numbers.reverse; console.log(reversed);
Solution
Step 1: Check how reverse is used
The code assignsnumbers.reversewithout parentheses, so it assigns the function itself, not the reversed array.Step 2: Correct usage of reverse()
To reverse the array, the method must be called with parentheses:numbers.reverse().Final Answer:
Missing parentheses to call reverse method. -> Option BQuick Check:
Call reverse() with () to execute [OK]
- Forgetting parentheses when calling methods
- Thinking reverse needs arguments
- Believing reverse is not a valid method
const data = [10, 20, 30, 40, 50];, which code snippet correctly reverses the array without modifying the original array?Solution
Step 1: Understand the problem requirement
We want to reverse the array but keep the original array unchanged.Step 2: Analyze each option
const reversed = data.reverse(); reverses the original array directly, modifying it.
const reversed = [...data].reverse(); creates a shallow copy using spread[...data]and then reverses the copy, preserving the original.
const reversed = data.sort().reverse(); sorts the original array first, changing order before reversing, and modifies original.
const reversed = data.map().reverse(); usesmap()incorrectly without a callback, causing error.Final Answer:
const reversed = [...data].reverse(); -> Option CQuick Check:
Copy then reverse to keep original safe [OK]
- Using reverse() directly modifies original array
- Sorting before reversing changes order incorrectly
- Using map() without a callback causes error
Ready to prove it?
Pass the test and this pattern moves unknown
