Challenge - 5 Problems
Tail Insert Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Tail Insert in a Linked List
What is the printed state of the linked list after inserting 4 and 5 at the end?
DSA Go
package main import "fmt" type Node struct { data int next *Node } type LinkedList struct { head *Node } func (l *LinkedList) InsertAtEnd(value int) { newNode := &Node{data: value} if l.head == nil { l.head = newNode return } current := l.head for current.next != nil { current = current.next } current.next = newNode } func (l *LinkedList) PrintList() { current := l.head for current != nil { fmt.Printf("%d -> ", current.data) current = current.next } fmt.Println("null") } func main() { list := LinkedList{} list.InsertAtEnd(1) list.InsertAtEnd(2) list.InsertAtEnd(3) list.InsertAtEnd(4) list.InsertAtEnd(5) list.PrintList() }
Attempts:
2 left
💡 Hint
Think about how nodes are added at the end and how the list is printed.
✗ Incorrect
The InsertAtEnd method adds new nodes at the tail. After inserting 1,2,3,4,5 in order, the list prints from head to tail showing 1 -> 2 -> 3 -> 4 -> 5 -> null.
❓ Predict Output
intermediateResulting List After Multiple Tail Inserts
What is the linked list state after these insertions at the end: 10, 20, 30?
DSA Go
package main import "fmt" type Node struct { data int next *Node } type LinkedList struct { head *Node } func (l *LinkedList) InsertAtEnd(value int) { newNode := &Node{data: value} if l.head == nil { l.head = newNode return } current := l.head for current.next != nil { current = current.next } current.next = newNode } func (l *LinkedList) PrintList() { current := l.head for current != nil { fmt.Printf("%d -> ", current.data) current = current.next } fmt.Println("null") } func main() { list := LinkedList{} list.InsertAtEnd(10) list.InsertAtEnd(20) list.InsertAtEnd(30) list.PrintList() }
Attempts:
2 left
💡 Hint
Nodes are added at the end, so order is preserved.
✗ Incorrect
Inserting 10, then 20, then 30 at the end results in the list 10 -> 20 -> 30 -> null.
🔧 Debug
advancedIdentify the Bug in Tail Insert Method
What error will occur when running this code that inserts at the end of a linked list?
DSA Go
package main import "fmt" type Node struct { data int next *Node } type LinkedList struct { head *Node } func (l *LinkedList) InsertAtEnd(value int) { newNode := &Node{data: value} current := l.head for current.next != nil { current = current.next } current.next = newNode } func (l *LinkedList) PrintList() { current := l.head for current != nil { fmt.Printf("%d -> ", current.data) current = current.next } fmt.Println("null") } func main() { list := LinkedList{} list.InsertAtEnd(1) list.PrintList() }
Attempts:
2 left
💡 Hint
Check what happens if the list is empty when InsertAtEnd is called.
✗ Incorrect
The InsertAtEnd method does not handle the case when the list is empty (head is nil). Accessing current.next causes a nil pointer dereference runtime error.
❓ Predict Output
advancedLinked List State After Tail Insertions and One Removal
What is the printed linked list after inserting 7, 8, 9 at the end and then removing the head node?
DSA Go
package main import "fmt" type Node struct { data int next *Node } type LinkedList struct { head *Node } func (l *LinkedList) InsertAtEnd(value int) { newNode := &Node{data: value} if l.head == nil { l.head = newNode return } current := l.head for current.next != nil { current = current.next } current.next = newNode } func (l *LinkedList) RemoveHead() { if l.head != nil { l.head = l.head.next } } func (l *LinkedList) PrintList() { current := l.head for current != nil { fmt.Printf("%d -> ", current.data) current = current.next } fmt.Println("null") } func main() { list := LinkedList{} list.InsertAtEnd(7) list.InsertAtEnd(8) list.InsertAtEnd(9) list.RemoveHead() list.PrintList() }
Attempts:
2 left
💡 Hint
Removing the head means the first inserted node is gone.
✗ Incorrect
After inserting 7,8,9, the list is 7 -> 8 -> 9 -> null. Removing the head removes 7, so the list becomes 8 -> 9 -> null.
🧠 Conceptual
expertTime Complexity of Tail Insert in Singly Linked List
What is the time complexity of inserting a node at the end (tail) of a singly linked list without a tail pointer?
Attempts:
2 left
💡 Hint
Think about how you find the last node in a singly linked list without extra pointers.
✗ Incorrect
Without a tail pointer, you must start at the head and move through each node to reach the end, which takes O(n) time.
