Challenge - 5 Problems
Intersection Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Intersection Detection with Different Length Lists
What is the output of the following C# code that finds the intersection point of two singly linked lists?
DSA C#
public class ListNode { public int val; public ListNode next; public ListNode(int x) { val = x; next = null; } } public class Solution { public ListNode GetIntersectionNode(ListNode headA, ListNode headB) { if (headA == null || headB == null) return null; ListNode a = headA, b = headB; while (a != b) { a = (a == null) ? headB : a.next; b = (b == null) ? headA : b.next; } return a; } } // Setup linked lists ListNode common = new ListNode(8); common.next = new ListNode(10); ListNode headA = new ListNode(3); headA.next = new ListNode(7); headA.next.next = common; ListNode headB = new ListNode(99); headB.next = new ListNode(1); headB.next.next = common; Solution sol = new Solution(); ListNode intersection = sol.GetIntersectionNode(headA, headB); System.Console.WriteLine(intersection != null ? intersection.val.ToString() : "null");
Attempts:
2 left
💡 Hint
Think about how the pointers switch lists to align lengths.
✗ Incorrect
The code uses two pointers that traverse both lists. When one pointer reaches the end, it switches to the other list's head. This aligns the pointers so they meet at the intersection node, which has value 8.
🧠 Conceptual
intermediateUnderstanding Pointer Movement in Intersection Detection
In the two-pointer approach to find the intersection of two linked lists, why do we switch the pointer to the other list's head when it reaches the end?
Attempts:
2 left
💡 Hint
Consider the difference in lengths of the two lists.
✗ Incorrect
Switching pointers to the other list's head makes both pointers traverse the combined length of both lists, so they meet at the intersection point or both reach null if no intersection.
❓ Predict Output
advancedOutput When No Intersection Exists
What will be printed by the following C# code when two linked lists do not intersect?
DSA C#
public class ListNode { public int val; public ListNode next; public ListNode(int x) { val = x; next = null; } } public class Solution { public ListNode GetIntersectionNode(ListNode headA, ListNode headB) { if (headA == null || headB == null) return null; ListNode a = headA, b = headB; while (a != b) { a = (a == null) ? headB : a.next; b = (b == null) ? headA : b.next; } return a; } } ListNode headA = new ListNode(1); headA.next = new ListNode(2); headA.next.next = new ListNode(3); ListNode headB = new ListNode(4); headB.next = new ListNode(5); headB.next.next = new ListNode(6); Solution sol = new Solution(); ListNode intersection = sol.GetIntersectionNode(headA, headB); System.Console.WriteLine(intersection != null ? intersection.val.ToString() : "null");
Attempts:
2 left
💡 Hint
If no intersection exists, what does the code return?
✗ Incorrect
When no intersection exists, both pointers eventually become null at the same time, causing the loop to end and returning null.
🔧 Debug
advancedIdentify the Error in Intersection Detection Code
What error will the following C# code produce when trying to find the intersection point of two linked lists?
DSA C#
public class ListNode { public int val; public ListNode next; public ListNode(int x) { val = x; next = null; } } public class Solution { public ListNode GetIntersectionNode(ListNode headA, ListNode headB) { ListNode a = headA, b = headB; while (a != b) { a = (a == null) ? headB : a.next; b = (b == null) ? headA : b.next; } return a; } }
Attempts:
2 left
💡 Hint
Check what happens when a or b is null before accessing next.
✗ Incorrect
Using 'a.next ?? headB' causes NullReferenceException when 'a' is null because 'a.next' is accessed without checking if 'a' is null.
🚀 Application
expertNumber of Nodes in Intersection Segment
Given two singly linked lists that intersect, what is the number of nodes in the intersection segment if the first intersecting node has value 15 and the lists share nodes with values [15, 30, 45]?
Attempts:
2 left
💡 Hint
Count all nodes from the intersection node to the end.
✗ Incorrect
The intersection segment includes all nodes starting from the first intersecting node to the end of the lists. Here, nodes with values 15, 30, and 45 are shared, so count is 3.
