Bird
Raised Fist0
DSA Cprogramming~20 mins

Traversal Forward and Backward in DSA C - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Forward Traversal in Doubly Linked List
What is the printed output of the following C code that traverses a doubly linked list forward?
DSA C
#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int data;
    struct Node* prev;
    struct Node* next;
} Node;

void printForward(Node* head) {
    Node* current = head;
    while (current != NULL) {
        printf("%d -> ", current->data);
        current = current->next;
    }
    printf("NULL\n");
}

int main() {
    Node* n1 = malloc(sizeof(Node));
    Node* n2 = malloc(sizeof(Node));
    Node* n3 = malloc(sizeof(Node));

    n1->data = 10; n1->prev = NULL; n1->next = n2;
    n2->data = 20; n2->prev = n1; n2->next = n3;
    n3->data = 30; n3->prev = n2; n3->next = NULL;

    printForward(n1);
    return 0;
}
A10 -> 20 -> 30 -> NULL
B30 -> 20 -> 10 -> NULL
C10 20 30 NULL
DNULL -> 10 -> 20 -> 30
Attempts:
2 left
💡 Hint
Follow the next pointers starting from the head node.
❓ Predict Output
intermediate
2:00remaining
Output of Backward Traversal in Doubly Linked List
What is the printed output of the following C code that traverses a doubly linked list backward?
DSA C
#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int data;
    struct Node* prev;
    struct Node* next;
} Node;

void printBackward(Node* tail) {
    Node* current = tail;
    while (current != NULL) {
        printf("%d -> ", current->data);
        current = current->prev;
    }
    printf("NULL\n");
}

int main() {
    Node* n1 = malloc(sizeof(Node));
    Node* n2 = malloc(sizeof(Node));
    Node* n3 = malloc(sizeof(Node));

    n1->data = 10; n1->prev = NULL; n1->next = n2;
    n2->data = 20; n2->prev = n1; n2->next = n3;
    n3->data = 30; n3->prev = n2; n3->next = NULL;

    printBackward(n3);
    return 0;
}
A30 -> 20 -> 10 -> NULL
B10 -> 20 -> 30 -> NULL
CNULL -> 30 -> 20 -> 10
D30 20 10 NULL
Attempts:
2 left
💡 Hint
Follow the prev pointers starting from the tail node.
🧠 Conceptual
advanced
1:00remaining
Understanding Traversal Pointers in Doubly Linked List
In a doubly linked list, which pointer is used to move backward during traversal?
Ahead pointer
Bnext pointer
Cprev pointer
Dtail pointer
Attempts:
2 left
💡 Hint
Think about which pointer links to the previous node.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Backward Traversal Code
What error will occur when running this backward traversal code snippet?
DSA C
#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int data;
    struct Node* prev;
    struct Node* next;
} Node;

void printBackward(Node* tail) {
    Node* current = tail;
    while (current != NULL) {
        printf("%d -> ", current->next->data);
        current = current->prev;
    }
    printf("NULL\n");
}
ACompilation error due to missing semicolon
BSegmentation fault (accessing NULL pointer)
CNo error, prints backward list correctly
DInfinite loop
Attempts:
2 left
💡 Hint
Check what happens when current->next is NULL.
🚀 Application
expert
3:00remaining
Result of Mixed Forward and Backward Traversal
Given a doubly linked list with nodes 1 <-> 2 <-> 3 <-> 4, what is the output after this code runs? 1. Traverse forward from head and print nodes. 2. Then traverse backward from tail and print nodes. Assume print format: "data -> " and ends with "NULL".
DSA C
#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int data;
    struct Node* prev;
    struct Node* next;
} Node;

void printForward(Node* head) {
    Node* current = head;
    while (current != NULL) {
        printf("%d -> ", current->data);
        current = current->next;
    }
    printf("NULL\n");
}

void printBackward(Node* tail) {
    Node* current = tail;
    while (current != NULL) {
        printf("%d -> ", current->data);
        current = current->prev;
    }
    printf("NULL\n");
}

int main() {
    Node* n1 = malloc(sizeof(Node));
    Node* n2 = malloc(sizeof(Node));
    Node* n3 = malloc(sizeof(Node));
    Node* n4 = malloc(sizeof(Node));

    n1->data = 1; n1->prev = NULL; n1->next = n2;
    n2->data = 2; n2->prev = n1; n2->next = n3;
    n3->data = 3; n3->prev = n2; n3->next = n4;
    n4->data = 4; n4->prev = n3; n4->next = NULL;

    printForward(n1);
    printBackward(n4);
    return 0;
}
A
NULL -&gt; 1 -&gt; 2 -&gt; 3 -&gt; 4
NULL -&gt; 4 -&gt; 3 -&gt; 2 -&gt; 1
B
4 -&gt; 3 -&gt; 2 -&gt; 1 -&gt; NULL
1 -&gt; 2 -&gt; 3 -&gt; 4 -&gt; NULL
C
1 2 3 4 NULL
4 3 2 1 NULL
D
1 -&gt; 2 -&gt; 3 -&gt; 4 -&gt; NULL
4 -&gt; 3 -&gt; 2 -&gt; 1 -&gt; NULL
Attempts:
2 left
💡 Hint
First print forward from head, then backward from tail.

Practice

(1/5)
1. What does forward traversal in a linked list mean?
easy
A. Visiting nodes from the first node to the last node
B. Visiting nodes from the last node to the first node
C. Skipping every alternate node while visiting
D. Visiting nodes randomly

Solution

  1. Step 1: Understand forward traversal

    Forward traversal means starting at the first node and moving to the next node repeatedly until the end.
  2. 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.
  3. Final Answer:

    Visiting nodes from the first node to the last node -> Option A
  4. Quick Check:

    Forward traversal = first to last [OK]
Hint: Forward means start to end in order [OK]
Common Mistakes:
  • Confusing forward with backward traversal
  • Thinking traversal is random
  • Assuming skipping nodes is traversal
2. Which of the following is the correct syntax to traverse a singly linked list forward in C?
easy
A. for(Node* curr = head; curr != NULL; curr = curr->prev) { /* process curr */ }
B. for(Node* curr = head; curr != NULL; curr = curr->next) { /* process curr */ }
C. while(head != NULL) { head = head->prev; }
D. for(Node* curr = tail; curr != NULL; curr = curr->prev) { /* process curr */ }

Solution

  1. Step 1: Identify forward traversal syntax

    Forward traversal in singly linked list uses next pointer starting from head.
  2. 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.
  3. Final Answer:

    for(Node* curr = head; curr != NULL; curr = curr->next) { /* process curr */ } -> Option B
  4. Quick Check:

    Use next pointer from head for forward traversal [OK]
Hint: Use next pointer from head for forward traversal [OK]
Common Mistakes:
  • Using prev pointer in singly linked list
  • Starting from tail in singly linked list
  • Using incorrect loop conditions
3. What will be the output of the following code snippet that traverses a doubly linked list backward?
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);
medium
A. 1 2 3
B. 3 1 2
C. 3 2 1
D. 2 3 1

Solution

  1. Step 1: Understand backward traversal in doubly linked list

    Starting from tail (3), moving prev pointer visits nodes 3, 2, then 1.
  2. Step 2: Trace the output

    Prints 3, then 2, then 1 with spaces.
  3. Final Answer:

    3 2 1 -> Option C
  4. Quick Check:

    Backward traversal prints tail to head [OK]
Hint: Backward traversal prints from tail to head [OK]
Common Mistakes:
  • Confusing order of printing
  • Using next pointer instead of prev
  • Starting from head instead of tail
4. Identify the error in the following code that attempts to traverse a doubly linked list backward:
void traverseBackward(Node* tail) {
  Node* curr = tail;
  while(curr->next != NULL) {
    printf("%d ", curr->data);
    curr = curr->prev;
  }
}
medium
A. The printf statement is missing a newline character
B. The pointer curr should start from head, not tail
C. The code should use curr = curr->next instead of curr = curr->prev
D. The loop condition should check curr != NULL, not curr->next != NULL

Solution

  1. Step 1: Analyze loop condition for backward traversal

    Backward traversal moves using prev pointer until curr becomes NULL.
  2. Step 2: Identify incorrect condition

    Checking curr->next != NULL is wrong; it should be curr != NULL to avoid null pointer access.
  3. Final Answer:

    The loop condition should check curr != NULL, not curr->next != NULL -> Option D
  4. Quick Check:

    Loop until curr is NULL in backward traversal [OK]
Hint: Check curr != NULL in loops, not curr->next for backward traversal [OK]
Common Mistakes:
  • Using next pointer condition in backward traversal
  • Starting from head instead of tail
  • Ignoring null pointer checks
5. Given a doubly linked list with nodes containing integers, write a function to print the list forward and then backward without using extra space. Which approach correctly achieves this?
hard
A. Traverse from head to tail printing data, then traverse from tail to head printing data
B. Traverse from head to tail printing data twice
C. Traverse from tail to head printing data, then traverse from head to tail printing data
D. Traverse from head to tail printing data, then reverse the list and print forward again

Solution

  1. Step 1: Understand traversal without extra space

    Use existing next and prev pointers to traverse forward and backward.
  2. 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.
  3. Final Answer:

    Traverse from head to tail printing data, then traverse from tail to head printing data -> Option A
  4. Quick Check:

    Use head and tail pointers for forward and backward traversal [OK]
Hint: Print forward from head, backward from tail without changing list [OK]
Common Mistakes:
  • Reversing list unnecessarily
  • Printing forward twice
  • Using extra space like arrays or stacks
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard