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 at the end?
DSA Go
package main import "fmt" type Node struct { value int prev *Node next *Node } type DoublyLinkedList struct { head *Node tail *Node } func (dll *DoublyLinkedList) InsertEnd(val int) { newNode := &Node{value: val} if dll.head == nil { dll.head = newNode dll.tail = newNode return } dll.tail.next = newNode newNode.prev = dll.tail dll.tail = newNode } func (dll *DoublyLinkedList) PrintForward() { current := dll.head for current != nil { fmt.Printf("%d", current.value) if current.next != nil { fmt.Printf(" <-> ") } current = current.next } fmt.Println() } func main() { dll := DoublyLinkedList{} dll.InsertEnd(10) dll.InsertEnd(20) dll.InsertEnd(30) dll.PrintForward() }
Attempts:
2 left
💡 Hint
Think about how nodes are linked when inserted at the end and how PrintForward prints the list.
✗ Incorrect
The InsertEnd method adds nodes at the tail, so the list grows as 10, then 20, then 30. PrintForward prints from head to tail with <-> between nodes.
❓ Predict Output
intermediateValue of Tail Node After Insertions
After inserting nodes with values 5, 15, and 25 at the beginning of the doubly linked list, what is the value of the tail node?
DSA Go
package main import "fmt" type Node struct { value int prev *Node next *Node } type DoublyLinkedList struct { head *Node tail *Node } func (dll *DoublyLinkedList) InsertBeginning(val int) { newNode := &Node{value: val} if dll.head == nil { dll.head = newNode dll.tail = newNode return } newNode.next = dll.head dll.head.prev = newNode dll.head = newNode } func main() { dll := DoublyLinkedList{} dll.InsertBeginning(5) dll.InsertBeginning(15) dll.InsertBeginning(25) fmt.Println(dll.tail.value) }
Attempts:
2 left
💡 Hint
Inserting at the beginning shifts the head but tail remains the first inserted node.
✗ Incorrect
Inserting at the beginning adds new nodes before the current head. The tail remains the first inserted node, which is 5.
🔧 Debug
advancedIdentify the Bug in Node Removal Method
What error will occur when running this RemoveNode method on a doubly linked list if the node to remove is the tail?
DSA Go
func (dll *DoublyLinkedList) RemoveNode(node *Node) {
if node.prev != nil {
node.prev.next = node.next
} else {
dll.head = node.next
}
if node.next != nil {
node.next.prev = node.prev
} else {
dll.tail = node.prev
}
node = nil
}Attempts:
2 left
💡 Hint
Check how tail and head pointers are updated when removing nodes at ends.
✗ Incorrect
The method correctly updates head and tail pointers when removing nodes at ends. Setting node = nil only changes local pointer, no error occurs.
❓ Predict Output
advancedOutput After Reversing Doubly Linked List
What is the printed state of the doubly linked list after reversing it? Initial list is 1 <-> 2 <-> 3.
DSA Go
package main import "fmt" type Node struct { value int prev *Node next *Node } type DoublyLinkedList struct { head *Node tail *Node } func (dll *DoublyLinkedList) Reverse() { current := dll.head var temp *Node for current != nil { temp = current.prev current.prev = current.next current.next = temp current = current.prev } if temp != nil { dll.head = temp.prev } } func (dll *DoublyLinkedList) PrintForward() { current := dll.head for current != nil { fmt.Printf("%d", current.value) if current.next != nil { fmt.Printf(" <-> ") } current = current.next } fmt.Println() } func main() { dll := DoublyLinkedList{} dll.head = &Node{value: 1} node2 := &Node{value: 2} node3 := &Node{value: 3} dll.head.next = node2 node2.prev = dll.head node2.next = node3 node3.prev = node2 dll.tail = node3 dll.PrintForward() dll.Reverse() dll.PrintForward() }
Attempts:
2 left
💡 Hint
Reversing swaps next and prev pointers and updates head to old tail.
✗ Incorrect
The Reverse method swaps next and prev for each node and sets head to old tail, so the list prints reversed order.
🧠 Conceptual
expertMemory Management in Doubly Linked List Node Deletion
In Go, when a node is removed from a doubly linked list by updating pointers and setting the node variable to nil inside the removal function, what happens to the memory allocated for that node?
Attempts:
2 left
💡 Hint
Think about how Go manages memory and garbage collection.
✗ Incorrect
Go uses garbage collection. Setting node = nil removes one reference, but memory is freed only when no references remain.
