Traversal Forward and Backward in DSA C - Time & Space Complexity
We want to understand how long it takes to move through a list from start to end and then back from end to start.
How does the time needed change as the list gets bigger?
Analyze the time complexity of the following code snippet.
void traverseForwardBackward(int arr[], int n) {
// Traverse forward
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
// Traverse backward
for (int i = n - 1; i >= 0; i--) {
printf("%d ", arr[i]);
}
}
This code prints all elements of an array first from the start to the end, then from the end back to the start.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Two separate loops that each visit every element once.
- How many times: Each loop runs exactly n times, where n is the number of elements.
As the list size grows, 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: The total steps double as input size doubles, showing a linear growth pattern.
Time Complexity: O(n)
This means the time to complete the traversal grows directly in proportion to the number of elements.
[X] Wrong: "Because there are two loops, the time complexity is O(n²)."
[OK] Correct: The loops run one after another, not nested. So their times add up, not multiply.
Understanding how simple traversals add up helps you explain and optimize code clearly in interviews.
"What if we combined the forward and backward traversals into a single loop that prints both directions simultaneously? How would the time complexity change?"
Practice
Solution
Step 1: Understand forward traversal
Forward traversal means starting at the first node and moving to the next node repeatedly until the end.Step 2: Compare with options
Only Visiting nodes from the first node to the last node describes visiting nodes from first to last, which matches forward traversal.Final Answer:
Visiting nodes from the first node to the last node -> Option AQuick Check:
Forward traversal = first to last [OK]
- Confusing forward with backward traversal
- Thinking traversal is random
- Assuming skipping nodes is traversal
Solution
Step 1: Identify forward traversal syntax
Forward traversal in singly linked list uses next pointer starting from head.Step 2: Check options for correct pointer usage
for(Node* curr = head; curr != NULL; curr = curr->next) { /* process curr */ } uses curr = curr->next starting from head, which is correct.Final Answer:
for(Node* curr = head; curr != NULL; curr = curr->next) { /* process curr */ } -> Option BQuick Check:
Use next pointer from head for forward traversal [OK]
- Using prev pointer in singly linked list
- Starting from tail in singly linked list
- Using incorrect loop conditions
typedef struct Node {
int data;
struct Node* next;
struct Node* prev;
} Node;
void printBackward(Node* tail) {
Node* curr = tail;
while(curr != NULL) {
printf("%d ", curr->data);
curr = curr->prev;
}
}
// List: 1 <-> 2 <-> 3, tail points to node with data 3
printBackward(tail);Solution
Step 1: Understand backward traversal in doubly linked list
Starting from tail (3), moving prev pointer visits nodes 3, 2, then 1.Step 2: Trace the output
Prints 3, then 2, then 1 with spaces.Final Answer:
3 2 1 -> Option CQuick Check:
Backward traversal prints tail to head [OK]
- Confusing order of printing
- Using next pointer instead of prev
- Starting from head instead of tail
void traverseBackward(Node* tail) {
Node* curr = tail;
while(curr->next != NULL) {
printf("%d ", curr->data);
curr = curr->prev;
}
}Solution
Step 1: Analyze loop condition for backward traversal
Backward traversal moves using prev pointer until curr becomes NULL.Step 2: Identify incorrect condition
Checking curr->next != NULL is wrong; it should be curr != NULL to avoid null pointer access.Final Answer:
The loop condition should check curr != NULL, not curr->next != NULL -> Option DQuick Check:
Loop until curr is NULL in backward traversal [OK]
- Using next pointer condition in backward traversal
- Starting from head instead of tail
- Ignoring null pointer checks
Solution
Step 1: Understand traversal without extra space
Use existing next and prev pointers to traverse forward and backward.Step 2: Identify correct approach
Traverse from head to tail printing data, then traverse from tail to head printing data correctly traverses forward from head to tail, then backward from tail to head printing data without modifying list.Final Answer:
Traverse from head to tail printing data, then traverse from tail to head printing data -> Option AQuick Check:
Use head and tail pointers for forward and backward traversal [OK]
- Reversing list unnecessarily
- Printing forward twice
- Using extra space like arrays or stacks
Ready to prove it?
Pass the test and this pattern moves unknown
