Challenge - 5 Problems
Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput 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();
Attempts:
2 left
💡 Hint
Think about how the PrintForward method moves through the list.
✗ Incorrect
The PrintForward method starts at the head and moves next until null, printing each node's data. So it prints 10, then 20, then 30, then null.
❓ Predict Output
intermediateOutput 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();
Attempts:
2 left
💡 Hint
Backward traversal starts from the last node and moves prev.
✗ Incorrect
The PrintBackward method first moves to the last node, then prints data moving backward using prev pointers. So it prints 25, then 15, then 5, then null.
🧠 Conceptual
advancedUnderstanding Traversal Pointers in Doubly Linked List
In a doubly linked list, which pointer is used to move backward during traversal?
Attempts:
2 left
💡 Hint
Backward means going to the previous node.
✗ Incorrect
The prev pointer in each node points to the previous node, allowing backward traversal.
🔧 Debug
advancedIdentify 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");
Attempts:
2 left
💡 Hint
Think about where temp starts and what prev points to at head.
✗ Incorrect
Starting at head and moving prev causes temp.prev to be null immediately, so accessing temp.data next iteration causes NullReferenceException.
🚀 Application
expertResult 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();
Attempts:
2 left
💡 Hint
The forward loop breaks at node with data 3, then backward starts from there.
✗ Incorrect
Forward prints nodes 1, 2, 3 then breaks. Backward starts at node 3 and moves prev printing 3, 2, 1.
