Challenge - 5 Problems
Doubly Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after inserting nodes at the beginning
What is the printed state of the doubly linked list after inserting 10, then 20, then 30 at the beginning?
DSA Go
package main import "fmt" type Node struct { data int prev *Node next *Node } type DoublyLinkedList struct { head *Node } func (dll *DoublyLinkedList) InsertAtBeginning(data int) { newNode := &Node{data: data} if dll.head == nil { dll.head = newNode return } newNode.next = dll.head dll.head.prev = newNode dll.head = newNode } func (dll *DoublyLinkedList) PrintList() { current := dll.head for current != nil { fmt.Printf("%d", current.data) if current.next != nil { fmt.Print(" <-> ") } current = current.next } fmt.Println() } func main() { dll := DoublyLinkedList{} dll.InsertAtBeginning(10) dll.InsertAtBeginning(20) dll.InsertAtBeginning(30) dll.PrintList() }
Attempts:
2 left
💡 Hint
Remember, inserting at the beginning means the newest node becomes the head.
✗ Incorrect
Each new node is added before the current head, so the list order is last inserted to first inserted: 30, then 20, then 10.
🧠 Conceptual
intermediateUnderstanding pointers in doubly linked list insertion
When inserting a new node at the beginning of a doubly linked list, which pointers must be updated?
Attempts:
2 left
💡 Hint
Think about the direction of links in a doubly linked list.
✗ Incorrect
The new node's next must point to the old head, the old head's prev must point back to the new node, and the head pointer must be updated to the new node.
🔧 Debug
advancedFind the bug in this insertion code
What error will this code cause when inserting at the beginning of a doubly linked list?
DSA Go
func (dll *DoublyLinkedList) InsertAtBeginning(data int) {
newNode := &Node{data: data}
newNode.next = dll.head
dll.head.prev = newNode
dll.head = newNode
}Attempts:
2 left
💡 Hint
What happens if dll.head is nil?
✗ Incorrect
If the list is empty, dll.head is nil, so dll.head.prev causes a nil pointer dereference panic.
📝 Syntax
advancedIdentify the syntax error in this insertion function
Which option correctly fixes the syntax error in this code snippet?
DSA Go
func (dll *DoublyLinkedList) InsertAtBeginning(data int) { newNode := &Node{data: data} if dll.head == nil { dll.head = newNode } else { newNode.next = dll.head dll.head.prev = newNode dll.head = newNode }
Attempts:
2 left
💡 Hint
Check for matching braces in function definitions.
✗ Incorrect
The function is missing a closing brace '}', causing a syntax error.
🚀 Application
expertResulting list after mixed insertions
Given an empty doubly linked list, we perform these operations in order:
1. Insert 5 at beginning
2. Insert 10 at beginning
3. Insert 15 at end
4. Insert 20 at beginning
What is the final printed list?
DSA Go
package main import "fmt" type Node struct { data int prev *Node next *Node } type DoublyLinkedList struct { head *Node tail *Node } func (dll *DoublyLinkedList) InsertAtBeginning(data int) { newNode := &Node{data: data} if dll.head == nil { dll.head = newNode dll.tail = newNode return } newNode.next = dll.head dll.head.prev = newNode dll.head = newNode } func (dll *DoublyLinkedList) InsertAtEnd(data int) { newNode := &Node{data: data} if dll.tail == nil { dll.head = newNode dll.tail = newNode return } dll.tail.next = newNode newNode.prev = dll.tail dll.tail = newNode } func (dll *DoublyLinkedList) PrintList() { current := dll.head for current != nil { fmt.Printf("%d", current.data) if current.next != nil { fmt.Print(" <-> ") } current = current.next } fmt.Println() } func main() { dll := DoublyLinkedList{} dll.InsertAtBeginning(5) dll.InsertAtBeginning(10) dll.InsertAtEnd(15) dll.InsertAtBeginning(20) dll.PrintList() }
Attempts:
2 left
💡 Hint
Track the head and tail pointers carefully after each insertion.
✗ Incorrect
Insertions at beginning add nodes before head; insertion at end adds after tail. Final order is 20, 10, 5, 15.
