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
Given two singly linked lists that intersect, what is the value of the intersection node printed by the code?
DSA Typescript
class ListNode { val: number; next: ListNode | null; constructor(val?: number, next?: ListNode | null) { this.val = val === undefined ? 0 : val; this.next = next === undefined ? null : next; } } function getIntersectionNode(headA: ListNode | null, headB: ListNode | null): ListNode | null { if (!headA || !headB) return null; let a: ListNode | null = headA; let b: ListNode | null = headB; while (a !== b) { a = a ? a.next : headB; b = b ? b.next : headA; } return a; } // Creating intersecting lists const intersect = new ListNode(8, new ListNode(10)); const listA = new ListNode(3, new ListNode(7, intersect)); const listB = new ListNode(99, new ListNode(1, intersect)); const intersection = getIntersectionNode(listA, listB); console.log(intersection ? intersection.val : null);
Attempts:
2 left
💡 Hint
Trace the pointers moving through each list and switching heads when reaching the end.
✗ Incorrect
The function moves two pointers through both lists. When one pointer reaches the end, it switches to the other list's head. They meet at the intersection node with value 8.
❓ Predict Output
intermediateOutput when no intersection exists
What does the following code print when two linked lists do NOT intersect?
DSA Typescript
class ListNode { val: number; next: ListNode | null; constructor(val?: number, next?: ListNode | null) { this.val = val === undefined ? 0 : val; this.next = next === undefined ? null : next; } } function getIntersectionNode(headA: ListNode | null, headB: ListNode | null): ListNode | null { if (!headA || !headB) return null; let a: ListNode | null = headA; let b: ListNode | null = headB; while (a !== b) { a = a ? a.next : headB; b = b ? b.next : headA; } return a; } const listA = new ListNode(1, new ListNode(2, new ListNode(3))); const listB = new ListNode(4, new ListNode(5)); const intersection = getIntersectionNode(listA, listB); console.log(intersection ? intersection.val : null);
Attempts:
2 left
💡 Hint
If lists do not share nodes, pointers will both become null eventually.
✗ Incorrect
Since the lists do not intersect, 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 of two linked lists?
DSA Typescript
class ListNode { val: number; next: ListNode | null; constructor(val?: number, next?: ListNode | null) { this.val = val === undefined ? 0 : val; this.next = next === undefined ? null : next; } } function getIntersectionNode(headA: ListNode | null, headB: ListNode | null): ListNode | null { let a: ListNode | null = headA; let b: ListNode | null = headB; while (a !== b) { a = a ? a.next : headB; b = b ? b.next : headA; } return a; } const intersect = new ListNode(7); const listA = new ListNode(1, new ListNode(2, intersect)); const listB = new ListNode(3, intersect); const intersection = getIntersectionNode(listA, listB); console.log(intersection ? intersection.val : null);
Attempts:
2 left
💡 Hint
Check what happens when a or b is null before accessing .next
✗ Incorrect
The code tries to access a.next or b.next without checking if a or b is null, causing a TypeError.
❓ Predict Output
advancedLength difference approach output
What is the output of the code that finds the intersection node by first calculating lengths and then aligning the start pointers?
DSA Typescript
class ListNode { val: number; next: ListNode | null; constructor(val?: number, next?: ListNode | null) { this.val = val === undefined ? 0 : val; this.next = next === undefined ? null : next; } } function getLength(head: ListNode | null): number { let length = 0; let current = head; while (current) { length++; current = current.next; } return length; } function getIntersectionNode(headA: ListNode | null, headB: ListNode | null): ListNode | null { let lenA = getLength(headA); let lenB = getLength(headB); let a = headA; let b = headB; while (lenA > lenB) { a = a!.next; lenA--; } while (lenB > lenA) { b = b!.next; lenB--; } while (a !== b) { a = a!.next; b = b!.next; } return a; } const intersect = new ListNode(15, new ListNode(30)); const listA = new ListNode(10, new ListNode(12, intersect)); const listB = new ListNode(3, intersect); const intersection = getIntersectionNode(listA, listB); console.log(intersection ? intersection.val : null);
Attempts:
2 left
💡 Hint
Align the longer list's pointer to the same distance from the end as the shorter list.
✗ Incorrect
The code aligns the pointers by skipping nodes in the longer list, then moves both pointers until they meet at the intersection node with value 15.
🧠 Conceptual
expertWhy does the two-pointer approach guarantee intersection detection?
Why does the two-pointer approach (switching heads when reaching the end) always find the intersection node or return null if none exists?
Attempts:
2 left
💡 Hint
Think about how switching heads equalizes the path lengths.
✗ Incorrect
By switching heads, each pointer traverses both lists fully, so they align in distance from the intersection or end, ensuring they meet at the intersection or both become null.
