Challenge - 5 Problems
Intersection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateFind the intersection node value
What is the value of the node where the two linked lists intersect in the following code?
DSA Javascript
class ListNode { constructor(val) { this.val = val; this.next = null; } } // Create intersection node const intersect = new ListNode(8); intersect.next = new ListNode(10); // List A: 3 -> 7 -> 8 -> 10 -> null const headA = new ListNode(3); headA.next = new ListNode(7); headA.next.next = intersect; // List B: 99 -> 1 -> 8 -> 10 -> null const headB = new ListNode(99); headB.next = new ListNode(1); headB.next.next = intersect; function getIntersectionNode(headA, headB) { let pA = headA; let pB = headB; while (pA !== pB) { pA = pA ? pA.next : headB; pB = pB ? pB.next : headA; } return pA; } const intersection = getIntersectionNode(headA, headB); console.log(intersection ? intersection.val : null);
Attempts:
2 left
💡 Hint
The intersection node is the first common node shared by both lists.
✗ Incorrect
The function uses two pointers that traverse both lists. When they meet, it is the intersection node. Here, the node with value 8 is the first shared node.
❓ Predict Output
intermediateOutput when no intersection exists
What will be printed when the two linked lists do not intersect?
DSA Javascript
class ListNode { constructor(val) { this.val = val; this.next = null; } } // List A: 1 -> 2 -> 3 -> null const headA = new ListNode(1); headA.next = new ListNode(2); headA.next.next = new ListNode(3); // List B: 4 -> 5 -> 6 -> null const headB = new ListNode(4); headB.next = new ListNode(5); headB.next.next = new ListNode(6); function getIntersectionNode(headA, headB) { let pA = headA; let pB = headB; while (pA !== pB) { pA = pA ? pA.next : headB; pB = pB ? pB.next : headA; } return pA; } const intersection = getIntersectionNode(headA, headB); console.log(intersection ? intersection.val : null);
Attempts:
2 left
💡 Hint
If no nodes are shared, the function returns null.
✗ Incorrect
Since the lists do not share any node, the pointers will both become null after traversing both lists, so the function returns null.
🔧 Debug
advancedIdentify the error in intersection detection
What error does the following code produce when trying to find the intersection point?
DSA Javascript
function getIntersectionNode(headA, headB) {
let pA = headA;
let pB = headB;
while (pA !== pB) {
pA = pA.next || headB;
pB = pB.next || headA;
}
return pA;
}Attempts:
2 left
💡 Hint
Check what happens when pA or pB is null before accessing .next
✗ Incorrect
Using pA.next without checking if pA is null causes a TypeError when pA is null.
❓ Predict Output
advancedIntersection node with cycle in one list
What is the output of the code when one list has a cycle and the other intersects at a node inside the cycle?
DSA Javascript
class ListNode { constructor(val) { this.val = val; this.next = null; } } // Create cycle nodes const cycleNode1 = new ListNode(4); const cycleNode2 = new ListNode(5); const cycleNode3 = new ListNode(6); cycleNode1.next = cycleNode2; cycleNode2.next = cycleNode3; cycleNode3.next = cycleNode1; // cycle here // List A: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> cycle back to 4 const headA = new ListNode(1); headA.next = new ListNode(2); headA.next.next = new ListNode(3); headA.next.next.next = cycleNode1; // List B: 9 -> 5 -> 6 -> cycle back to 4 const headB = new ListNode(9); headB.next = cycleNode2; function getIntersectionNode(headA, headB) { let pA = headA; let pB = headB; let visited = new Set(); while (pA !== null && !visited.has(pA)) { visited.add(pA); pA = pA.next; } while (pB !== null) { if (visited.has(pB)) return pB; pB = pB.next; } return null; } const intersection = getIntersectionNode(headA, headB); console.log(intersection ? intersection.val : null);
Attempts:
2 left
💡 Hint
The intersection is the first common node visited by both lists.
✗ Incorrect
List B starts at 9 and then points to node with value 5, which is inside the cycle of List A. The first common node is 5.
🧠 Conceptual
expertWhy does the two-pointer approach find intersection?
Why does the two-pointer approach in the intersection detection algorithm work correctly for lists of different lengths?
Attempts:
2 left
💡 Hint
Think about how the pointers traverse both lists fully.
✗ Incorrect
By switching pointers to the other list after reaching the end, both pointers traverse the combined length of both lists, aligning them to meet at the intersection or null.
