Challenge - 5 Problems
Doubly Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Doubly Linked List Initialization
What is the printed state of the doubly linked list after initializing it with values 10, 20, 30 in order?
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) Append(value int) { newNode := &Node{data: value} 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.Print(current.data) if current.next != nil { fmt.Print(" <-> ") } current = current.next } fmt.Println(" <-> nil") } func main() { dll := DoublyLinkedList{} dll.Append(10) dll.Append(20) dll.Append(30) dll.PrintForward() }
Attempts:
2 left
💡 Hint
Remember that Append adds nodes at the end, so the order is the same as insertion.
✗ Incorrect
The Append method adds nodes at the tail. So the list prints from head to tail as 10 <-> 20 <-> 30 <-> nil.
❓ Predict Output
intermediateOutput after inserting at head in Doubly Linked List
What is the printed state of the doubly linked list after inserting 5 at the head of a list initialized with 10, 20, 30?
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) Append(value int) { newNode := &Node{data: value} 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) InsertAtHead(value int) { newNode := &Node{data: value} 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) PrintForward() { current := dll.head for current != nil { fmt.Print(current.data) if current.next != nil { fmt.Print(" <-> ") } current = current.next } fmt.Println(" <-> nil") } func main() { dll := DoublyLinkedList{} dll.Append(10) dll.Append(20) dll.Append(30) dll.InsertAtHead(5) dll.PrintForward() }
Attempts:
2 left
💡 Hint
InsertAtHead adds a new node before the current head.
✗ Incorrect
Inserting 5 at head places it before 10, so the list prints 5 <-> 10 <-> 20 <-> 30 <-> nil.
🔧 Debug
advancedIdentify the bug in doubly linked list Append method
What error will occur when running this Append method code for a doubly linked list?
DSA Go
func (dll *DoublyLinkedList) Append(value int) { newNode := &Node{data: value} if dll.head == nil { dll.head = newNode dll.tail = newNode return } dll.tail.next = newNode newNode.prev = dll.head dll.tail = newNode }
Attempts:
2 left
💡 Hint
Check the assignment of newNode.prev in relation to tail and head.
✗ Incorrect
newNode.prev should point to the current tail, not the head. Assigning newNode.prev = dll.head causes incorrect backward links.
❓ Predict Output
advancedOutput after removing a node from doubly linked list
What is the printed state of the doubly linked list after removing the node with value 20 from the list 10 <-> 20 <-> 30?
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) Append(value int) { newNode := &Node{data: value} 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) Remove(value int) { current := dll.head for current != nil { if current.data == value { if current.prev != nil { current.prev.next = current.next } else { dll.head = current.next } if current.next != nil { current.next.prev = current.prev } else { dll.tail = current.prev } return } current = current.next } } func (dll *DoublyLinkedList) PrintForward() { current := dll.head for current != nil { fmt.Print(current.data) if current.next != nil { fmt.Print(" <-> ") } current = current.next } fmt.Println(" <-> nil") } func main() { dll := DoublyLinkedList{} dll.Append(10) dll.Append(20) dll.Append(30) dll.Remove(20) dll.PrintForward() }
Attempts:
2 left
💡 Hint
Removing 20 should link 10 directly to 30.
✗ Incorrect
After removing 20, the list links 10 to 30, printing 10 <-> 30 <-> nil.
🧠 Conceptual
expertCorrect order of steps to initialize a doubly linked list with one node
Which is the correct sequence of steps to initialize a doubly linked list with a single node containing value 15?
Attempts:
2 left
💡 Hint
The node must be created first, then its pointers set, then list pointers assigned.
✗ Incorrect
First create the node (1), then set its prev and next to nil (4), then assign head (2) and tail (3) to this node.
