Challenge - 5 Problems
Doubly Linked List Insertion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after inserting at position 3 in doubly linked list
What is the printed state of the doubly linked list after inserting a node with value 25 at position 3?
DSA C#
class Node { public int data; public Node prev, next; public Node(int d) { data = d; prev = next = null; } } class DoublyLinkedList { public Node head; public void InsertAtPosition(int data, int position) { Node newNode = new Node(data); if (position == 1) { newNode.next = head; if (head != null) head.prev = newNode; head = newNode; return; } Node temp = head; for (int i = 1; i < position - 1 && temp != null; i++) { temp = temp.next; } if (temp == null) return; newNode.next = temp.next; if (temp.next != null) temp.next.prev = newNode; temp.next = newNode; newNode.prev = temp; } public string PrintList() { Node temp = head; string result = ""; while (temp != null) { result += temp.data + " <-> "; temp = temp.next; } result += "null"; return result; } } DoublyLinkedList dll = new DoublyLinkedList(); dll.InsertAtPosition(10, 1); dll.InsertAtPosition(20, 2); dll.InsertAtPosition(30, 3); dll.InsertAtPosition(25, 3); string output = dll.PrintList(); Console.WriteLine(output);
Attempts:
2 left
💡 Hint
Remember that position 3 means the new node goes after the second node.
✗ Incorrect
The list starts as 10 <-> 20 <-> 30. Inserting 25 at position 3 places it between 20 and 30.
❓ Predict Output
intermediateOutput after inserting at head in doubly linked list
What is the printed state of the doubly linked list after inserting a node with value 5 at position 1?
DSA C#
class Node { public int data; public Node prev, next; public Node(int d) { data = d; prev = next = null; } } class DoublyLinkedList { public Node head; public void InsertAtPosition(int data, int position) { Node newNode = new Node(data); if (position == 1) { newNode.next = head; if (head != null) head.prev = newNode; head = newNode; return; } Node temp = head; for (int i = 1; i < position - 1 && temp != null; i++) { temp = temp.next; } if (temp == null) return; newNode.next = temp.next; if (temp.next != null) temp.next.prev = newNode; temp.next = newNode; newNode.prev = temp; } public string PrintList() { Node temp = head; string result = ""; while (temp != null) { result += temp.data + " <-> "; temp = temp.next; } result += "null"; return result; } } DoublyLinkedList dll = new DoublyLinkedList(); dll.InsertAtPosition(10, 1); dll.InsertAtPosition(20, 2); dll.InsertAtPosition(30, 3); dll.InsertAtPosition(5, 1); string output = dll.PrintList(); Console.WriteLine(output);
Attempts:
2 left
💡 Hint
Inserting at position 1 means the new node becomes the new head.
✗ Incorrect
The new node with value 5 is inserted at the head, so it appears before 10.
🔧 Debug
advancedIdentify the error in insertion at position code
What error will occur when running this code to insert at position 4 in a list of 3 nodes?
DSA C#
class Node { public int data; public Node prev, next; public Node(int d) { data = d; prev = next = null; } } class DoublyLinkedList { public Node head; public void InsertAtPosition(int data, int position) { Node newNode = new Node(data); Node temp = head; for (int i = 1; i < position - 1; i++) { temp = temp.next; } newNode.next = temp.next; if (temp.next != null) temp.next.prev = newNode; temp.next = newNode; newNode.prev = temp; } } DoublyLinkedList dll = new DoublyLinkedList(); dll.InsertAtPosition(10, 1); dll.InsertAtPosition(20, 2); dll.InsertAtPosition(30, 3); dll.InsertAtPosition(40, 4);
Attempts:
2 left
💡 Hint
Check what happens if temp becomes null before accessing temp.next.
✗ Incorrect
The loop does not check if temp is null before accessing temp.next, so if position is beyond list length + 1, temp becomes null and accessing temp.next causes NullReferenceException.
❓ Predict Output
advancedOutput after inserting at tail in doubly linked list
What is the printed state of the doubly linked list after inserting a node with value 40 at position 5 in a list of 4 nodes?
DSA C#
class Node { public int data; public Node prev, next; public Node(int d) { data = d; prev = next = null; } } class DoublyLinkedList { public Node head; public void InsertAtPosition(int data, int position) { Node newNode = new Node(data); if (position == 1) { newNode.next = head; if (head != null) head.prev = newNode; head = newNode; return; } Node temp = head; for (int i = 1; i < position - 1 && temp != null; i++) { temp = temp.next; } if (temp == null) return; newNode.next = temp.next; if (temp.next != null) temp.next.prev = newNode; temp.next = newNode; newNode.prev = temp; } public string PrintList() { Node temp = head; string result = ""; while (temp != null) { result += temp.data + " <-> "; temp = temp.next; } result += "null"; return result; } } DoublyLinkedList dll = new DoublyLinkedList(); dll.InsertAtPosition(10, 1); dll.InsertAtPosition(20, 2); dll.InsertAtPosition(30, 3); dll.InsertAtPosition(35, 4); dll.InsertAtPosition(40, 5); string output = dll.PrintList(); Console.WriteLine(output);
Attempts:
2 left
💡 Hint
Inserting at position 5 in a 4-node list adds the node at the end.
✗ Incorrect
The new node 40 is added after 35, making it the new tail.
🧠 Conceptual
expertEffect of incorrect prev pointer update in doubly linked list insertion
What is the consequence of forgetting to update the prev pointer of the node after the inserted node in a doubly linked list insertion?
Attempts:
2 left
💡 Hint
Think about how doubly linked lists rely on both next and prev pointers for correct navigation.
✗ Incorrect
If the prev pointer of the next node is not updated to point back to the new node, the backward links break, causing traversal errors or infinite loops.
