Bird
Raised Fist0
DSA C#programming~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 after running the following C# code that traverses a doubly linked list forward?
DSA C#
public class Node {
    public int data;
    public Node next, prev;
    public Node(int d) { data = d; next = prev = null; }
}

public class DoublyLinkedList {
    public Node head;
    public void AddLast(int data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
            return;
        }
        Node temp = head;
        while (temp.next != null) temp = temp.next;
        temp.next = newNode;
        newNode.prev = temp;
    }
    public void PrintForward() {
        Node temp = head;
        while (temp != null) {
            System.Console.Write(temp.data + " -> ");
            temp = temp.next;
        }
        System.Console.WriteLine("null");
    }
}

var list = new DoublyLinkedList();
list.AddLast(10);
list.AddLast(20);
list.AddLast(30);
list.PrintForward();
A10 -> 20 -> 30 -> null
B30 -> 20 -> 10 -> null
Cnull -> 10 -> 20 -> 30
D10 -> 20 -> null
Attempts:
2 left
💡 Hint
Think about how the PrintForward method moves through the list.
❓ Predict Output
intermediate
2:00remaining
Output of Backward Traversal in Doubly Linked List
What is the printed output after running the following C# code that traverses a doubly linked list backward?
DSA C#
public class Node {
    public int data;
    public Node next, prev;
    public Node(int d) { data = d; next = prev = null; }
}

public class DoublyLinkedList {
    public Node head;
    public void AddLast(int data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
            return;
        }
        Node temp = head;
        while (temp.next != null) temp = temp.next;
        temp.next = newNode;
        newNode.prev = temp;
    }
    public void PrintBackward() {
        Node temp = head;
        if (temp == null) return;
        while (temp.next != null) temp = temp.next;
        while (temp != null) {
            System.Console.Write(temp.data + " -> ");
            temp = temp.prev;
        }
        System.Console.WriteLine("null");
    }
}

var list = new DoublyLinkedList();
list.AddLast(5);
list.AddLast(15);
list.AddLast(25);
list.PrintBackward();
A25 -> 15 -> 5 -> null
B5 -> 15 -> 25 -> null
Cnull -> 25 -> 15 -> 5
D25 -> 15 -> null
Attempts:
2 left
💡 Hint
Backward traversal starts from the last node and moves prev.
🧠 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?
Anext pointer
Bhead pointer
Cprev pointer
Dtail pointer
Attempts:
2 left
💡 Hint
Backward means going to the previous node.
🔧 Debug
advanced
1:30remaining
Identify the Bug in Backward Traversal Code
What error will occur when running this backward traversal code snippet on a doubly linked list?
DSA C#
Node temp = head;
while (temp != null) {
    temp = temp.prev;
    System.Console.Write(temp.data + " -> ");
}
System.Console.WriteLine("null");
AInfinite loop because temp never becomes null
BNullReferenceException because temp.prev is null at head
CNo error, prints list backward correctly
DSyntax error due to missing semicolon
Attempts:
2 left
💡 Hint
Think about where temp starts and what prev points to at head.
🚀 Application
expert
2:30remaining
Result of Mixed Forward and Backward Traversal
Given a doubly linked list with nodes 1, 2, 3, 4 added in order, what is the output of this traversal code?
DSA C#
Node temp = head;
System.Console.Write("Forward: ");
while (temp != null) {
    System.Console.Write(temp.data + " ");
    if (temp.data == 3) break;
    temp = temp.next;
}
System.Console.WriteLine();
System.Console.Write("Backward: ");
while (temp != null) {
    System.Console.Write(temp.data + " ");
    temp = temp.prev;
}
System.Console.WriteLine();
A
Forward: 1 2 3 
Backward: 3 4 
B
Forward: 1 2 3 4 
Backward: 4 3 2 1 
C
Forward: 1 2 
Backward: 2 1 
D
Forward: 1 2 3 
Backward: 3 2 1 
Attempts:
2 left
💡 Hint
The forward loop breaks at node with data 3, then backward starts from there.

Practice

(1/5)
1. What does Traversal Forward and Backward mean in the context of a doubly linked list?
easy
A. Visiting nodes from the start to the end and then from the end back to the start
B. Only visiting nodes from the start to the end
C. Visiting nodes randomly without order
D. Visiting nodes only from the end to the start

Solution

  1. Step 1: Understand traversal directions

    Traversal forward means visiting nodes from the first node to the last node. Traversal backward means visiting nodes from the last node back to the first node.
  2. Step 2: Apply to doubly linked list

    A doubly linked list allows moving in both directions because each node has links to the next and previous nodes.
  3. Final Answer:

    Visiting nodes from the start to the end and then from the end back to the start -> Option A
  4. Quick Check:

    Forward and backward traversal = visiting nodes both ways [OK]
Hint: Think of walking forward then turning back [OK]
Common Mistakes:
  • Confusing traversal with random access
  • Thinking traversal is only forward
  • Ignoring backward links in doubly linked list
2. Which of the following is the correct syntax to traverse a doubly linked list forward in C#?
easy
A. for(Node current = tail; current != null; current = current.Next) { /* process current */ }
B. for(Node current = head; current != null; current = current.Next) { /* process current */ }
C. for(Node current = head; current != null; current = current.Prev) { /* process current */ }
D. for(Node current = tail; current != null; current = current.Prev) { /* process current */ }

Solution

  1. Step 1: Identify forward traversal

    Forward traversal starts at the head and moves to the next node repeatedly until null.
  2. Step 2: Check syntax correctness

    for(Node current = head; current != null; current = current.Next) { /* process current */ } correctly starts at head and moves using current = current.Next. Other options either start at tail or move backward.
  3. Final Answer:

    for(Node current = head; current != null; current = current.Next) { /* process current */ } -> Option B
  4. Quick Check:

    Forward traversal uses Next pointer from head [OK]
Hint: Forward means start at head and use Next [OK]
Common Mistakes:
  • Starting at tail for forward traversal
  • Using Prev pointer for forward traversal
  • Confusing Next and Prev pointers
3. Given the doubly linked list with nodes containing values 1, 2, 3, 4, what is the output of this forward traversal code?
Node current = head;
while(current != null) {
    Console.Write(current.Value + " -> ");
    current = current.Next;
}
Console.WriteLine("null");
medium
A. null
B. 4 -> 3 -> 2 -> 1 -> null
C. 1 -> 2 -> 3 -> 4 -> null
D. 1 -> 2 -> 3 -> null

Solution

  1. Step 1: Understand the list structure

    The list nodes are 1 linked to 2, then 3, then 4, in order.
  2. Step 2: Trace the forward traversal

    Starting at head (1), print 1, move to 2, print 2, move to 3, print 3, move to 4, print 4, then current becomes null and loop ends.
  3. Final Answer:

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

    Forward traversal prints nodes in order [OK]
Hint: Follow Next pointers from head to null [OK]
Common Mistakes:
  • Printing backward order instead of forward
  • Missing last node in output
  • Printing null before nodes
4. Identify the error in this backward traversal code of a doubly linked list:
Node current = head;
while(current != null) {
    Console.Write(current.Value + " <- ");
    current = current.Prev;
}
Console.WriteLine("null");
medium
A. Missing null check in loop
B. Using Prev pointer instead of Next
C. Incorrect print format
D. Starting traversal from head instead of tail

Solution

  1. Step 1: Understand backward traversal

    Backward traversal should start from the tail node, not the head.
  2. Step 2: Analyze given code

    The code starts at head and tries to move backward using Prev, but head.Prev is null, so loop ends immediately.
  3. Final Answer:

    Starting traversal from head instead of tail -> Option D
  4. Quick Check:

    Backward traversal must start at tail [OK]
Hint: Backward means start at tail, not head [OK]
Common Mistakes:
  • Starting backward traversal at head
  • Confusing Prev and Next pointers
  • Ignoring list ends (null pointers)
5. You have a doubly linked list with nodes: 10 <-> 20 <-> 30 <-> 40. Write a method to print the list forward and then backward without using extra data structures. What will be the output?
hard
A. 10 -> 20 -> 30 -> 40 -> null 40 -> 30 -> 20 -> 10 -> null
B. 40 -> 30 -> 20 -> 10 -> null 10 -> 20 -> 30 -> 40 -> null
C. 10 -> 20 -> 30 -> 40 -> null null
D. null 40 -> 30 -> 20 -> 10 -> null

Solution

  1. Step 1: Traverse forward from head to tail

    Start at head (10), print each node's value moving via Next until null: 10 -> 20 -> 30 -> 40 -> null.
  2. Step 2: Traverse backward from tail to head

    After reaching tail (40), move backward using Prev pointer, printing: 40 -> 30 -> 20 -> 10 -> null.
  3. Final Answer:

    10 -> 20 -> 30 -> 40 -> null 40 -> 30 -> 20 -> 10 -> null -> Option A
  4. Quick Check:

    Forward then backward traversal prints nodes in both directions [OK]
Hint: Print forward using Next, backward using Prev [OK]
Common Mistakes:
  • Trying to traverse backward from head
  • Printing null before nodes
  • Using extra data structures unnecessarily
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