Challenge - 5 Problems
Doubly Linked List Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after inserting nodes at the end
What is the printed state of the doubly linked list after inserting 10, 20, and 30 at the end?
DSA Go
package main import "fmt" type Node struct { data int prev *Node next *Node } type DoublyLinkedList struct { head *Node } func (dll *DoublyLinkedList) InsertAtEnd(data int) { newNode := &Node{data: data} if dll.head == nil { dll.head = newNode return } temp := dll.head for temp.next != nil { temp = temp.next } temp.next = newNode newNode.prev = temp } func (dll *DoublyLinkedList) PrintList() { temp := dll.head for temp != nil { fmt.Print(temp.data) if temp.next != nil { fmt.Print(" <-> ") } temp = temp.next } fmt.Println(" <-> null") } func main() { dll := DoublyLinkedList{} dll.InsertAtEnd(10) dll.InsertAtEnd(20) dll.InsertAtEnd(30) dll.PrintList() }
Attempts:
2 left
💡 Hint
Remember that inserting at the end adds the new node after the last node.
✗ Incorrect
The InsertAtEnd method adds nodes at the end, so the list grows as 10, then 10 <-> 20, then 10 <-> 20 <-> 30. The PrintList method prints nodes from head to tail.
❓ Predict Output
intermediateOutput after inserting into empty list
What is the printed state of the doubly linked list after inserting a single node with value 5 at the end?
DSA Go
package main import "fmt" type Node struct { data int prev *Node next *Node } type DoublyLinkedList struct { head *Node } func (dll *DoublyLinkedList) InsertAtEnd(data int) { newNode := &Node{data: data} if dll.head == nil { dll.head = newNode return } temp := dll.head for temp.next != nil { temp = temp.next } temp.next = newNode newNode.prev = temp } func (dll *DoublyLinkedList) PrintList() { temp := dll.head for temp != nil { fmt.Print(temp.data) if temp.next != nil { fmt.Print(" <-> ") } temp = temp.next } fmt.Println(" <-> null") } func main() { dll := DoublyLinkedList{} dll.InsertAtEnd(5) dll.PrintList() }
Attempts:
2 left
💡 Hint
Inserting into an empty list sets the head to the new node.
✗ Incorrect
Since the list was empty, the new node becomes the head. The print shows the single node followed by null.
🔧 Debug
advancedIdentify the bug in InsertAtEnd method
What error will occur when running this InsertAtEnd method if the list is empty?
DSA Go
func (dll *DoublyLinkedList) InsertAtEnd(data int) { newNode := &Node{data: data} temp := dll.head for temp.next != nil { temp = temp.next } temp.next = newNode newNode.prev = temp }
Attempts:
2 left
💡 Hint
Check what happens when dll.head is nil and you try to access temp.next.
✗ Incorrect
If dll.head is nil, temp is nil. Accessing temp.next causes a runtime nil pointer dereference error.
❓ Predict Output
advancedOutput after multiple InsertAtEnd calls
What is the printed state of the doubly linked list after these InsertAtEnd calls: InsertAtEnd(1), InsertAtEnd(2), InsertAtEnd(3), InsertAtEnd(4)?
DSA Go
package main import "fmt" type Node struct { data int prev *Node next *Node } type DoublyLinkedList struct { head *Node } func (dll *DoublyLinkedList) InsertAtEnd(data int) { newNode := &Node{data: data} if dll.head == nil { dll.head = newNode return } temp := dll.head for temp.next != nil { temp = temp.next } temp.next = newNode newNode.prev = temp } func (dll *DoublyLinkedList) PrintList() { temp := dll.head for temp != nil { fmt.Print(temp.data) if temp.next != nil { fmt.Print(" <-> ") } temp = temp.next } fmt.Println(" <-> null") } func main() { dll := DoublyLinkedList{} dll.InsertAtEnd(1) dll.InsertAtEnd(2) dll.InsertAtEnd(3) dll.InsertAtEnd(4) dll.PrintList() }
Attempts:
2 left
💡 Hint
Nodes are added at the end, so order is preserved.
✗ Incorrect
Each InsertAtEnd adds a node after the last node, so the list grows as 1, 1 <-> 2, 1 <-> 2 <-> 3, 1 <-> 2 <-> 3 <-> 4.
🧠 Conceptual
expertWhy is the prev pointer important in InsertAtEnd?
In a doubly linked list, what is the main reason to set the prev pointer of the new node when inserting at the end?
Attempts:
2 left
💡 Hint
Think about what the prev pointer lets you do in a doubly linked list.
✗ Incorrect
The prev pointer links the new node back to the previous node, enabling traversal in both directions.
