Traversal Forward and Backward in DSA Typescript - Time & Space Complexity
When we move through a list or array from start to end and then back again, we want to know how long this takes as the list grows.
We ask: How does the time to go forward and backward change when the list gets bigger?
Analyze the time complexity of the following code snippet.
function traverseForwardBackward(arr: number[]): void {
for (let i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
for (let j = arr.length - 1; j >= 0; j--) {
console.log(arr[j]);
}
}
This code goes through the array from start to end, then from end back to start, printing each element.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Two separate loops each visiting every element once.
- How many times: Each loop runs n times, where n is the array length.
As the array gets bigger, the total steps grow roughly twice as fast because we go forward and backward.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 20 (10 forward + 10 backward) |
| 100 | 200 (100 forward + 100 backward) |
| 1000 | 2000 (1000 forward + 1000 backward) |
Pattern observation: Operations grow linearly with input size, doubling because of two passes.
Time Complexity: O(n)
This means the time grows directly in proportion to the size of the array, even though we do two passes.
[X] Wrong: "Because there are two loops, the time complexity is O(n²)."
[OK] Correct: The loops run one after another, not nested. So the total steps add up, not multiply.
Understanding how multiple passes over data add up helps you explain and optimize code clearly in interviews.
"What if we combined the forward and backward traversal into a single loop that processes two elements per step? How would the time complexity change?"
Practice
Solution
Step 1: Understand linked list structure
A linked list has nodes connected from head to tail in one direction.Step 2: Define forward traversal
Forward traversal means visiting nodes starting at the head and moving to the tail in order.Final Answer:
Visiting nodes from the head to the tail in order -> Option DQuick Check:
Forward traversal = head to tail [OK]
- Confusing forward with backward traversal
- Thinking traversal is random
- Assuming skipping nodes is traversal
Solution
Step 1: Identify forward traversal loop
Forward traversal starts at index 0 and goes up to length-1.Step 2: Check loop conditions
for(let i = 0; i < arr.length; i++) { console.log(arr[i]); } loops from 0 to less than arr.length, printing each element correctly.Final Answer:
for(let i = 0; i < arr.length; i++) { console.log(arr[i]); } -> Option BQuick Check:
Forward loop index from 0 to length-1 [OK]
- Starting index at 1 instead of 0
- Using i <= arr.length causing out-of-bounds
- Looping backward for forward traversal
const arr = [10, 20, 30];
for(let i = arr.length - 1; i >= 0; i--) {
console.log(arr[i]);
}Solution
Step 1: Understand loop start and end
The loop starts at index 2 (arr.length - 1) and goes down to 0.Step 2: Trace output values
It prints arr[2] = 30, arr[1] = 20, arr[0] = 10 in that order.Final Answer:
30 20 10 -> Option AQuick Check:
Backward traversal prints 30 20 10 [OK]
- Thinking output is forward order
- Missing last element at index 0
- Assuming error due to loop
let current = head;
while(current !== null) {
console.log(current.value);
current = current.prev;
}Solution
Step 1: Understand backward traversal in linked list
Backward traversal uses prev pointers starting from the tail node.Step 2: Check starting node
The code starts at head, but to go backward, it must start at tail.Final Answer:
It should start from tail, not head -> Option CQuick Check:
Backward traversal starts at tail [OK]
- Starting backward traversal at head
- Using next instead of prev for backward
- Wrong loop condition check
Solution
Step 1: Understand doubly linked list structure
Each node has next and prev pointers; head has no prev, tail has no next.Step 2: Plan backward print from head
To print backward from head, first move forward to tail using next pointers.Step 3: Traverse backward from tail
From tail, use prev pointers to print nodes backward to head.Final Answer:
Traverse forward to tail, then traverse backward from tail to head -> Option AQuick Check:
Backward print from head requires forward then backward traversal [OK]
- Trying to go backward directly from head
- Using next pointers to print backward
- Reversing list unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
