Challenge - 5 Problems
Master of Insert at Middle Specific Position
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after inserting a node at position 3 in a singly linked list
What is the printed linked list after inserting a node with value 99 at position 3 in the list 10 -> 20 -> 30 -> 40 -> null?
DSA Go
package main import "fmt" type Node struct { value int next *Node } func insertAtPosition(head *Node, val int, pos int) *Node { newNode := &Node{value: val} if pos == 1 { newNode.next = head return newNode } current := head for i := 1; i < pos-1 && current != nil; i++ { current = current.next } if current == nil { return head } newNode.next = current.next current.next = newNode return head } func printList(head *Node) { for head != nil { fmt.Printf("%d -> ", head.value) head = head.next } fmt.Println("null") } func main() { head := &Node{10, &Node{20, &Node{30, &Node{40, nil}}}} head = insertAtPosition(head, 99, 3) printList(head) }
Attempts:
2 left
💡 Hint
Remember that position 3 means the new node should be after the second node.
✗ Incorrect
The function inserts the new node with value 99 after the second node (value 20), so the list becomes 10 -> 20 -> 99 -> 30 -> 40 -> null.
❓ Predict Output
intermediateResult of inserting at position beyond list length
What is the printed linked list after trying to insert a node with value 50 at position 10 in the list 1 -> 2 -> 3 -> null?
DSA Go
package main import "fmt" type Node struct { value int next *Node } func insertAtPosition(head *Node, val int, pos int) *Node { newNode := &Node{value: val} if pos == 1 { newNode.next = head return newNode } current := head for i := 1; i < pos-1 && current != nil; i++ { current = current.next } if current == nil { return head } newNode.next = current.next current.next = newNode return head } func printList(head *Node) { for head != nil { fmt.Printf("%d -> ", head.value) head = head.next } fmt.Println("null") } func main() { head := &Node{1, &Node{2, &Node{3, nil}}} head = insertAtPosition(head, 50, 10) printList(head) }
Attempts:
2 left
💡 Hint
If the position is beyond the list length, the list remains unchanged.
✗ Incorrect
Since position 10 is beyond the length of the list, the function returns the original list without insertion.
🔧 Debug
advancedIdentify the runtime error in insertAtPosition function
What runtime error will occur when running this code if the position is 1 and the list is empty (head is nil)?
DSA Go
package main import "fmt" type Node struct { value int next *Node } func insertAtPosition(head *Node, val int, pos int) *Node { newNode := &Node{value: val} if pos == 1 { newNode.next = head return newNode } current := head for i := 1; i < pos-1 && current != nil; i++ { current = current.next } newNode.next = current.next current.next = newNode return head } func main() { var head *Node = nil head = insertAtPosition(head, 5, 1) fmt.Println(head.value) }
Attempts:
2 left
💡 Hint
Check the condition for position 1 and empty list.
✗ Incorrect
When position is 1, the function sets newNode.next to head (which can be nil) and returns newNode. This works fine even if head is nil, so no runtime error occurs.
❓ Predict Output
advancedOutput after inserting at position 1 in a non-empty list
What is the printed linked list after inserting a node with value 7 at position 1 in the list 4 -> 5 -> 6 -> null?
DSA Go
package main import "fmt" type Node struct { value int next *Node } func insertAtPosition(head *Node, val int, pos int) *Node { newNode := &Node{value: val} if pos == 1 { newNode.next = head return newNode } current := head for i := 1; i < pos-1 && current != nil; i++ { current = current.next } if current == nil { return head } newNode.next = current.next current.next = newNode return head } func printList(head *Node) { for head != nil { fmt.Printf("%d -> ", head.value) head = head.next } fmt.Println("null") } func main() { head := &Node{4, &Node{5, &Node{6, nil}}} head = insertAtPosition(head, 7, 1) printList(head) }
Attempts:
2 left
💡 Hint
Inserting at position 1 means the new node becomes the new head.
✗ Incorrect
The new node with value 7 is inserted at the start, so the list becomes 7 -> 4 -> 5 -> 6 -> null.
🧠 Conceptual
expertNumber of nodes after multiple insertions at middle positions
Starting with an empty singly linked list, you insert nodes with values 1, 2, 3, 4 in this order at positions 1, 2, 2, and 3 respectively. How many nodes does the list have after all insertions?
Attempts:
2 left
💡 Hint
Track the list length after each insertion carefully.
✗ Incorrect
Insertions add one node each time. Starting empty, after 4 insertions, the list has 4 nodes.
