Challenge - 5 Problems
Doubly Linked List Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Doubly Linked List after Insertions
What is the printed state of the doubly linked list after inserting nodes with values 10, 20, and 30 in that order at the end?
DSA C#
public class Node { public int Data; public Node Prev; public Node Next; public Node(int data) { Data = data; Prev = null; Next = null; } } public class DoublyLinkedList { public Node Head; public Node Tail; public void InsertAtEnd(int data) { Node newNode = new Node(data); if (Head == null) { Head = Tail = newNode; } else { Tail.Next = newNode; newNode.Prev = Tail; Tail = newNode; } } public void PrintList() { Node current = Head; while (current != null) { System.Console.Write(current.Data); if (current.Next != null) System.Console.Write(" <-> "); current = current.Next; } System.Console.WriteLine(" <-> null"); } } var list = new DoublyLinkedList(); list.InsertAtEnd(10); list.InsertAtEnd(20); list.InsertAtEnd(30); list.PrintList();
Attempts:
2 left
💡 Hint
Remember that InsertAtEnd adds nodes at the tail, so the order is the same as insertion.
✗ Incorrect
Nodes are inserted at the end, so the list prints in the order 10, then 20, then 30, connected by <-> arrows, ending with null.
❓ Predict Output
intermediateState of Doubly Linked List after Deleting Head
Given a doubly linked list with nodes 5 <-> 15 <-> 25 <-> null, what is the printed state after deleting the head node?
DSA C#
public class Node { public int Data; public Node Prev; public Node Next; public Node(int data) { Data = data; Prev = null; Next = null; } } public class DoublyLinkedList { public Node Head; public Node Tail; public void InsertAtEnd(int data) { Node newNode = new Node(data); if (Head == null) { Head = Tail = newNode; } else { Tail.Next = newNode; newNode.Prev = Tail; Tail = newNode; } } public void DeleteHead() { if (Head == null) return; if (Head == Tail) { Head = Tail = null; } else { Head = Head.Next; Head.Prev = null; } } public void PrintList() { Node current = Head; while (current != null) { System.Console.Write(current.Data); if (current.Next != null) System.Console.Write(" <-> "); current = current.Next; } System.Console.WriteLine(" <-> null"); } } var list = new DoublyLinkedList(); list.InsertAtEnd(5); list.InsertAtEnd(15); list.InsertAtEnd(25); list.DeleteHead(); list.PrintList();
Attempts:
2 left
💡 Hint
Deleting the head removes the first node and updates the head pointer.
✗ Incorrect
After deleting the head (5), the new head is 15, followed by 25, so the list prints 15 <-> 25 <-> null.
🔧 Debug
advancedIdentify the Bug in InsertAtBeginning Method
What is the bug in the InsertAtBeginning method of this doubly linked list implementation?
DSA C#
public class Node { public int Data; public Node Prev; public Node Next; public Node(int data) { Data = data; Prev = null; Next = null; } } public class DoublyLinkedList { public Node Head; public Node Tail; public void InsertAtBeginning(int data) { Node newNode = new Node(data); if (Head == null) { Head = newNode; Tail = newNode; } else { newNode.Next = Head; Head.Prev = newNode; // Missing update of Head pointer } } }
Attempts:
2 left
💡 Hint
Check if the head pointer points to the new first node after insertion.
✗ Incorrect
The method forgets to update Head to newNode after linking, so Head still points to old first node.
❓ Predict Output
advancedOutput after Reversing a Doubly Linked List
What is the printed state of the doubly linked list after reversing it? Initial list: 1 <-> 2 <-> 3 <-> null
DSA C#
public class Node { public int Data; public Node Prev; public Node Next; public Node(int data) { Data = data; Prev = null; Next = null; } } public class DoublyLinkedList { public Node Head; public Node Tail; public void InsertAtEnd(int data) { Node newNode = new Node(data); if (Head == null) { Head = Tail = newNode; } else { Tail.Next = newNode; newNode.Prev = Tail; Tail = newNode; } } public void Reverse() { Node current = Head; Node temp = null; while (current != null) { temp = current.Prev; current.Prev = current.Next; current.Next = temp; current = current.Prev; } if (temp != null) { Head = temp.Prev; } } public void PrintList() { Node current = Head; while (current != null) { System.Console.Write(current.Data); if (current.Next != null) System.Console.Write(" <-> "); current = current.Next; } System.Console.WriteLine(" <-> null"); } } var list = new DoublyLinkedList(); list.InsertAtEnd(1); list.InsertAtEnd(2); list.InsertAtEnd(3); list.Reverse(); list.PrintList();
Attempts:
2 left
💡 Hint
Reversing swaps next and prev pointers and updates head to last node.
✗ Incorrect
The Reverse method correctly swaps pointers and updates Head to the last node, so list prints reversed order.
🧠 Conceptual
expertMemory Usage in Doubly Linked List vs Singly Linked List
Which statement correctly explains the difference in memory usage between a doubly linked list and a singly linked list?
Attempts:
2 left
💡 Hint
Consider how many pointers each node stores in both lists.
✗ Incorrect
Each node in a doubly linked list stores two pointers (Prev and Next), so it uses more memory than a singly linked list node which stores only one pointer (Next).
