Challenge - 5 Problems
Node Structure Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Linked List Node Insertion
What is the printed output of the linked list after inserting nodes in this Go code?
DSA Go
package main import "fmt" type Node struct { value int next *Node } func main() { head := &Node{value: 1} second := &Node{value: 2} third := &Node{value: 3} head.next = second second.next = third newNode := &Node{value: 0, next: head} head = newNode current := head for current != nil { fmt.Print(current.value) if current.next != nil { fmt.Print(" -> ") } current = current.next } fmt.Println(" -> null") }
Attempts:
2 left
💡 Hint
Think about how the new node is linked before the current head.
✗ Incorrect
The new node with value 0 is inserted before the original head node (value 1). So the list starts at 0, then 1, 2, 3.
❓ Predict Output
intermediateOutput after Reversing a Singly Linked List
What is the output of the linked list after running this reversal code?
DSA Go
package main import "fmt" type Node struct { value int next *Node } func reverse(head *Node) *Node { var prev *Node = nil current := head for current != nil { nextNode := current.next current.next = prev prev = current current = nextNode } return prev } func main() { head := &Node{value: 1} head.next = &Node{value: 2} head.next.next = &Node{value: 3} head = reverse(head) current := head for current != nil { fmt.Print(current.value) if current.next != nil { fmt.Print(" -> ") } current = current.next } fmt.Println(" -> null") }
Attempts:
2 left
💡 Hint
Reversing changes the direction of the next pointers.
✗ Incorrect
The reversal function changes the next pointers so the list order is reversed from 1->2->3 to 3->2->1.
🔧 Debug
advancedIdentify the Bug in Node Pointer Assignment
What error will this Go code produce when trying to print the linked list?
DSA Go
package main import "fmt" type Node struct { value int next *Node } func main() { var head *Node head.value = 1 head.next = nil fmt.Println(head.value) }
Attempts:
2 left
💡 Hint
Check if head is initialized before accessing its fields.
✗ Incorrect
head is a nil pointer. Accessing head.value causes a runtime panic due to nil pointer dereference.
🧠 Conceptual
advancedEffect of Changing Node Pointer in Function
Given this Go code, what will be the output after calling modifyNode?
DSA Go
package main import "fmt" type Node struct { value int next *Node } func modifyNode(n *Node) { n = &Node{value: 99} } func main() { head := &Node{value: 1} modifyNode(head) fmt.Println(head.value) }
Attempts:
2 left
💡 Hint
Consider if the original pointer is changed or only the local copy.
✗ Incorrect
The function modifies the local copy of the pointer, not the original. So head.value remains 1.
🚀 Application
expertDetermine the Number of Nodes in a Circular Linked List
What is the output of this Go program that counts nodes in a circular linked list?
DSA Go
package main import "fmt" type Node struct { value int next *Node } func countNodes(head *Node) int { if head == nil { return 0 } count := 1 current := head.next for current != nil && current != head { count++ current = current.next } return count } func main() { node1 := &Node{value: 1} node2 := &Node{value: 2} node3 := &Node{value: 3} node1.next = node2 node2.next = node3 node3.next = node1 fmt.Println(countNodes(node1)) }
Attempts:
2 left
💡 Hint
The loop stops when it reaches the head again.
✗ Incorrect
The function counts nodes until it loops back to the head, so it counts all 3 nodes.
