Traversal Forward and Backward in DSA Javascript - Time & Space Complexity
Traversal means visiting each item in a list one by one. Here, we look at going forward and backward through a list.
We want to know how the time taken grows as the list gets bigger.
Analyze the time complexity of the following code snippet.
const list = [1, 2, 3, 4, 5];
// Forward traversal
for (let i = 0; i < list.length; i++) {
console.log(list[i]);
}
// Backward traversal
for (let i = list.length - 1; i >= 0; i--) {
console.log(list[i]);
}
This code visits each element in the list first from start to end, then from end to start.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing each element in the list once per traversal.
- How many times: Forward loop runs n times; backward loop runs n times; total 2n times.
As the list size grows, the number of steps grows roughly twice as fast because we do two full traversals.
| 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 traversals.
Time Complexity: O(n)
This means the time grows in a straight line with the number of items, even though we do two passes.
[X] Wrong: "Doing two loops means the time complexity is O(n²)."
[OK] Correct: Each loop runs separately over the list, so times add up (n + n), not multiply. This keeps it linear, not quadratic.
Understanding how simple loops add up helps you explain and optimize code clearly. This skill shows you grasp how programs scale with data size.
"What if we combined the forward and backward traversal into a single loop that visits two elements per step? How would the time complexity change?"
Practice
forward?Solution
Step 1: Understand forward traversal
Forward traversal means starting at the first node (head) and moving to the next node repeatedly until the end.Step 2: Compare with other options
Backward traversal is the opposite, from last to first. Skipping or random visiting is not traversal.Final Answer:
Visiting nodes from the first node to the last node in order -> Option BQuick Check:
Forward traversal = first to last [OK]
- Confusing forward with backward traversal
- Thinking traversal skips nodes
- Assuming traversal is random
arr forward in JavaScript?Solution
Step 1: Identify forward traversal syntax
Forward traversal starts at index 0 and goes up to index arr.length - 1, so i from 0 to less than arr.length.Step 2: Check each option
for (let i = 0; i < arr.length; i++) { console.log(arr[i]); } uses i = 0 to i < arr.length, correct. for (let i = arr.length; i >= 0; i--) { console.log(arr[i]); } and D count backward. for (let i = 1; i <= arr.length; i++) { console.log(arr[i]); } starts at 1 and accesses arr[arr.length], which is undefined.Final Answer:
for (let i = 0; i < arr.length; i++) { console.log(arr[i]); } -> Option AQuick Check:
Forward array loop = i from 0 to length-1 [OK]
- Starting loop at 1 instead of 0
- Using <= instead of < in loop condition
- 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: Analyze loop indices
The loop starts at i = arr.length - 1 = 2, then decrements i until 0, so indices 2, 1, 0.Step 2: Print elements in reverse order
arr[2] = 30, arr[1] = 20, arr[0] = 10, so output is 30 20 10.Final Answer:
30 20 10 -> Option CQuick Check:
Backward loop prints elements in reverse [OK]
- Confusing forward and backward indices
- Expecting output in original order
- Off-by-one errors in loop condition
nums:for (let i = nums.length; i >= 0; i--) {
console.log(nums[i]);
}Solution
Step 1: Check loop start and direction
Array indices go from 0 to nums.length - 1. Starting at nums.length is out of bounds.Step 2: Correct loop start and decrement
Loop should start at nums.length - 1 and decrement i to 0 to traverse backward correctly.Final Answer:
Loop should start at nums.length - 1 and decrement i -> Option DQuick Check:
Backward loop start = length-1, decrement i [OK]
- Starting loop at length causing undefined access
- Incrementing index in backward loop
- Wrong loop condition allowing infinite loop
Solution
Step 1: Understand doubly linked list structure
Doubly linked lists have two pointers per node: next (forward) and prev (backward).Step 2: Use pointers for traversal
Forward traversal uses next pointers; backward traversal uses prev pointers efficiently.Final Answer:
Using next pointers for forward and prev pointers for backward traversal -> Option AQuick Check:
Doubly linked list uses next and prev pointers [OK]
- Assuming single pointer can traverse both ways
- Ignoring prev pointers in backward traversal
- Thinking traversal can be random
Ready to prove it?
Pass the test and this pattern moves unknown
