Challenge - 5 Problems
Head Insert Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after inserting nodes at the beginning?
Consider a singly linked list initially empty. We insert nodes with values 10, 20, and 30 at the beginning in that order. What is the final state of the list?
DSA Go
package main import "fmt" type Node struct { data int next *Node } func insertAtBeginning(head *Node, val int) *Node { newNode := &Node{data: val, next: head} return newNode } func printList(head *Node) { for head != nil { fmt.Print(head.data) if head.next != nil { fmt.Print(" -> ") } head = head.next } fmt.Println(" -> null") } func main() { var head *Node = nil head = insertAtBeginning(head, 10) head = insertAtBeginning(head, 20) head = insertAtBeginning(head, 30) printList(head) }
Attempts:
2 left
💡 Hint
Remember, inserting at the beginning means the newest node becomes the new head.
✗ Incorrect
Each insertion at the beginning places the new node before the current head. So inserting 10 first makes list: 10 -> null. Then inserting 20 at beginning makes: 20 -> 10 -> null. Finally inserting 30 at beginning makes: 30 -> 20 -> 10 -> null.
🧠 Conceptual
intermediateWhat happens to the head pointer after inserting at the beginning?
When inserting a new node at the beginning of a singly linked list, which statement is true about the head pointer?
Attempts:
2 left
💡 Hint
Think about which node is the first after insertion.
✗ Incorrect
Inserting at the beginning means the new node becomes the first node. So the head pointer must be updated to point to this new node.
🔧 Debug
advancedFind the bug in this head insert function
This Go function is supposed to insert a new node at the beginning of a singly linked list. What is the bug?
DSA Go
func insertAtBeginning(head *Node, val int) *Node { newNode := &Node{data: val} newNode.next = nil return newNode }
Attempts:
2 left
💡 Hint
Check what the new node's next pointer should be after insertion.
✗ Incorrect
The new node's next pointer must point to the current head to maintain the list. Setting it to nil breaks the chain.
📝 Syntax
advancedWhich option correctly inserts a node at the beginning?
Choose the correct Go code snippet that inserts a new node with value val at the beginning of a singly linked list with head pointer head.
Attempts:
2 left
💡 Hint
Remember to link the new node to the old head and update head.
✗ Incorrect
Option A correctly creates a new node, sets its next to head, and updates head to newNode. Option A tries to assign head but does not update the original head pointer outside the function. Option A incorrectly sets head.next which is wrong for head insert. Option A sets next to nil, breaking the list.
🚀 Application
expertAfter multiple head inserts, what is the sum of all node values?
Starting with an empty singly linked list, you insert the values 5, 15, 25, 35 at the beginning in that order. What is the sum of all node values in the final list?
Attempts:
2 left
💡 Hint
Sum all inserted values regardless of order.
✗ Incorrect
Sum of 5 + 15 + 25 + 35 = 80. The order of insertion does not affect the sum.
