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
What is the printed state of the doubly linked list after inserting value 99 at position 3?
DSA Go
package main import "fmt" type Node struct { data int prev *Node next *Node } type DoublyLinkedList struct { head *Node } func (dll *DoublyLinkedList) InsertAtPosition(data int, pos int) { newNode := &Node{data: data} if pos == 1 { newNode.next = dll.head if dll.head != nil { dll.head.prev = newNode } dll.head = newNode return } current := dll.head for i := 1; current != nil && i < pos-1; i++ { current = current.next } if current == nil { return } newNode.next = current.next if current.next != nil { current.next.prev = newNode } current.next = newNode newNode.prev = current } func (dll *DoublyLinkedList) PrintList() { current := dll.head for current != nil { fmt.Print(current.data) if current.next != nil { fmt.Print(" <-> ") } current = current.next } fmt.Println() } func main() { dll := &DoublyLinkedList{} dll.InsertAtPosition(10, 1) dll.InsertAtPosition(20, 2) dll.InsertAtPosition(30, 3) dll.InsertAtPosition(40, 4) dll.InsertAtPosition(99, 3) dll.PrintList() }
Attempts:
2 left
💡 Hint
Remember that position 3 means the new node goes after the second node.
✗ Incorrect
The new node with value 99 is inserted after the node with value 20 (position 2), so it becomes the third node.
❓ Predict Output
intermediateOutput after inserting at head position
What is the printed state of the doubly linked list after inserting value 5 at position 1 (head)?
DSA Go
package main import "fmt" type Node struct { data int prev *Node next *Node } type DoublyLinkedList struct { head *Node } func (dll *DoublyLinkedList) InsertAtPosition(data int, pos int) { newNode := &Node{data: data} if pos == 1 { newNode.next = dll.head if dll.head != nil { dll.head.prev = newNode } dll.head = newNode return } current := dll.head for i := 1; current != nil && i < pos-1; i++ { current = current.next } if current == nil { return } newNode.next = current.next if current.next != nil { current.next.prev = newNode } current.next = newNode newNode.prev = current } func (dll *DoublyLinkedList) PrintList() { current := dll.head for current != nil { fmt.Print(current.data) if current.next != nil { fmt.Print(" <-> ") } current = current.next } fmt.Println() } func main() { dll := &DoublyLinkedList{} dll.InsertAtPosition(10, 1) dll.InsertAtPosition(20, 2) dll.InsertAtPosition(30, 3) dll.InsertAtPosition(5, 1) dll.PrintList() }
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, pushing the existing nodes forward.
🔧 Debug
advancedIdentify the bug causing incorrect insertion at tail
The following code tries to insert a node at position 5 in a list of 4 nodes but fails to insert correctly. What is the bug?
DSA Go
func (dll *DoublyLinkedList) InsertAtPosition(data int, pos int) { newNode := &Node{data: data} if pos == 1 { newNode.next = dll.head if dll.head != nil { dll.head.prev = newNode } dll.head = newNode return } current := dll.head for i := 1; current != nil && i < pos-1; i++ { current = current.next } if current == nil { return } newNode.next = current.next if current.next != nil { current.next.prev = newNode } current.next = newNode newNode.prev = current }
Attempts:
2 left
💡 Hint
Check what happens when pos is exactly one more than the current list length.
✗ Incorrect
If pos is beyond the current list length + 1, current becomes nil and insertion is skipped. The code does not allow insertion at tail if pos is exactly list length + 1.
❓ Predict Output
advancedOutput after multiple insertions at various positions
What is the printed state of the doubly linked list after these insertions?
Insert 1 at pos 1
Insert 2 at pos 2
Insert 3 at pos 2
Insert 4 at pos 4
Insert 5 at pos 3
DSA Go
package main import "fmt" type Node struct { data int prev *Node next *Node } type DoublyLinkedList struct { head *Node } func (dll *DoublyLinkedList) InsertAtPosition(data int, pos int) { newNode := &Node{data: data} if pos == 1 { newNode.next = dll.head if dll.head != nil { dll.head.prev = newNode } dll.head = newNode return } current := dll.head for i := 1; current != nil && i < pos-1; i++ { current = current.next } if current == nil { return } newNode.next = current.next if current.next != nil { current.next.prev = newNode } current.next = newNode newNode.prev = current } func (dll *DoublyLinkedList) PrintList() { current := dll.head for current != nil { fmt.Print(current.data) if current.next != nil { fmt.Print(" <-> ") } current = current.next } fmt.Println() } func main() { dll := &DoublyLinkedList{} dll.InsertAtPosition(1, 1) dll.InsertAtPosition(2, 2) dll.InsertAtPosition(3, 2) dll.InsertAtPosition(4, 4) dll.InsertAtPosition(5, 3) dll.PrintList() }
Attempts:
2 left
💡 Hint
Trace each insertion step carefully, updating the list each time.
✗ Incorrect
After all insertions, the list is: 1 (pos1), 3 (pos2), 2 (pos3), 5 (pos4), 4 (pos5).
🧠 Conceptual
expertWhy is updating both prev and next pointers important in doubly linked list insertion?
When inserting a new node at a specific position in a doubly linked list, why must we update both the prev and next pointers of the involved nodes?
Attempts:
2 left
💡 Hint
Think about how you move forward and backward in the list.
✗ Incorrect
Doubly linked lists allow moving forward and backward. If prev or next pointers are not updated correctly, traversal in one direction will break.
