Challenge - 5 Problems
Intersection Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Intersection Point Detection
What will be the output of the following Go code that finds the intersection point of two linked lists?
DSA Go
package main import "fmt" type ListNode struct { Val int Next *ListNode } func getIntersectionNode(headA, headB *ListNode) *ListNode { if headA == nil || headB == nil { return nil } a, b := headA, headB for a != b { if a == nil { a = headB } else { a = a.Next } if b == nil { b = headA } else { b = b.Next } } return a } func main() { // Create intersecting lists: // List A: 1 -> 2 -> 3 -> 4 -> 5 // List B: 9 -> 4 -> 5 (4 and 5 are shared nodes) shared := &ListNode{Val: 4, Next: &ListNode{Val: 5, Next: nil}} headA := &ListNode{Val: 1, Next: &ListNode{Val: 2, Next: &ListNode{Val: 3, Next: shared}}} headB := &ListNode{Val: 9, Next: shared} intersection := getIntersectionNode(headA, headB) if intersection != nil { fmt.Println(intersection.Val) } else { fmt.Println("No intersection") } }
Attempts:
2 left
💡 Hint
Think about where the two lists start to share nodes.
✗ Incorrect
The two lists intersect starting at the node with value 4, so the function returns that node.
🧠 Conceptual
intermediateUnderstanding the Two Pointer Technique
Why does the two pointer technique used in finding the intersection point of two linked lists work?
Attempts:
2 left
💡 Hint
Consider what happens when each pointer reaches the end of its list.
✗ Incorrect
Switching heads makes both pointers traverse the combined length of both lists, so they align at the intersection or both reach nil.
🔧 Debug
advancedIdentify the Bug in Intersection Detection
What error will this Go code produce when trying to find the intersection point of two linked lists?
DSA Go
func getIntersectionNode(headA, headB *ListNode) *ListNode {
a, b := headA, headB
for a != b {
a = a.Next
b = b.Next
if a == nil {
a = headB
}
if b == nil {
b = headA
}
}
return a
}Attempts:
2 left
💡 Hint
Check the order of pointer moves and nil checks inside the loop.
✗ Incorrect
The code moves pointers before checking for nil, causing nil pointer dereference when accessing Next of nil.
❓ Predict Output
advancedOutput When Lists Do Not Intersect
What will be printed by this Go program when two linked lists do not intersect?
DSA Go
package main import "fmt" type ListNode struct { Val int Next *ListNode } func getIntersectionNode(headA, headB *ListNode) *ListNode { if headA == nil || headB == nil { return nil } a, b := headA, headB for a != b { if a == nil { a = headB } else { a = a.Next } if b == nil { b = headA } else { b = b.Next } } return a } func main() { headA := &ListNode{Val: 1, Next: &ListNode{Val: 2, Next: nil}} headB := &ListNode{Val: 3, Next: &ListNode{Val: 4, Next: nil}} intersection := getIntersectionNode(headA, headB) if intersection != nil { fmt.Println(intersection.Val) } else { fmt.Println("No intersection") } }
Attempts:
2 left
💡 Hint
The lists have no shared nodes.
✗ Incorrect
Since the lists do not share any nodes, the function returns nil and prints 'No intersection'.
🚀 Application
expertFind Intersection Node Value After Modifications
Given two linked lists intersecting at node with value 7, after modifying the second list by adding nodes before the intersection, what is the value of the intersection node found by the algorithm?
DSA Go
package main import "fmt" type ListNode struct { Val int Next *ListNode } func getIntersectionNode(headA, headB *ListNode) *ListNode { if headA == nil || headB == nil { return nil } a, b := headA, headB for a != b { if a == nil { a = headB } else { a = a.Next } if b == nil { b = headA } else { b = b.Next } } return a } func main() { shared := &ListNode{Val: 7, Next: &ListNode{Val: 8, Next: nil}} headA := &ListNode{Val: 1, Next: &ListNode{Val: 2, Next: shared}} headB := &ListNode{Val: 3, Next: &ListNode{Val: 4, Next: &ListNode{Val: 5, Next: shared}}} intersection := getIntersectionNode(headA, headB) if intersection != nil { fmt.Println(intersection.Val) } else { fmt.Println("No intersection") } }
Attempts:
2 left
💡 Hint
The intersection node is shared regardless of nodes added before it.
✗ Incorrect
Adding nodes before the intersection in one list does not change the intersection node value found by the algorithm.
