Challenge - 5 Problems
Intersection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Find the intersection node value
Given two singly linked lists that intersect, what is the value of the node where they intersect?
DSA C
struct Node {
int data;
struct Node* next;
};
// List A: 1 -> 2 -> 3 -> 4 -> 5 -> NULL
// List B: 9 -> 4 -> 5 -> NULL (4 and 5 are shared nodes)
// Intersection at node with value 4
int getIntersectionNode(struct Node* headA, struct Node* headB) {
struct Node *a = headA, *b = headB;
while (a != b) {
a = (a == NULL) ? headB : a->next;
b = (b == NULL) ? headA : b->next;
}
return a ? a->data : -1;
}Attempts:
2 left
💡 Hint
Think about how pointers move through both lists and meet at the intersection.
✗ Incorrect
The function uses two pointers that traverse both lists. When they meet, it is the intersection node. Here, the intersection node has value 4.
❓ Predict Output
intermediate2:00remaining
Output of intersection detection with no intersection
What is the output of the function when two linked lists do not intersect?
DSA C
struct Node {
int data;
struct Node* next;
};
int getIntersectionNode(struct Node* headA, struct Node* headB) {
struct Node *a = headA, *b = headB;
while (a != b) {
a = (a == NULL) ? headB : a->next;
b = (b == NULL) ? headA : b->next;
}
return a ? a->data : -1;
}
// List A: 1 -> 2 -> 3 -> NULL
// List B: 4 -> 5 -> 6 -> NULL
// No intersectionAttempts:
2 left
💡 Hint
If no intersection exists, the pointers will both become NULL at the same time.
✗ Incorrect
When no intersection exists, both pointers eventually become NULL simultaneously, so the function returns -1.
🧠 Conceptual
advanced2:00remaining
Why does the two-pointer technique work for intersection detection?
Why does moving two pointers through both lists and switching heads when reaching NULL guarantee they meet at the intersection node?
Attempts:
2 left
💡 Hint
Think about the total distance each pointer travels.
✗ Incorrect
Each pointer traverses both lists fully, so they cover equal distances. This alignment causes them to meet at the intersection node or both become NULL if no intersection.
🔧 Debug
advanced2:00remaining
Identify the bug in intersection detection code
What is the bug in this code snippet for detecting intersection of two linked lists?
DSA C
int getIntersectionNode(struct Node* headA, struct Node* headB) { struct Node *a = headA, *b = headB; while (a != NULL && b != NULL) { if (a == b) return a->data; a = a->next; b = b->next; } return -1; }
Attempts:
2 left
💡 Hint
Think about what happens if lists have different lengths.
✗ Incorrect
The loop stops when either pointer reaches NULL, missing the chance to switch heads and align pointers for intersection detection.
🚀 Application
expert2:00remaining
Number of nodes in intersection part
Given two intersecting singly linked lists, how many nodes are in the shared intersection part if the first intersection node has value 7 and the lists end at node with value 10?
DSA C
// List A: 3 -> 6 -> 9 -> 7 -> 8 -> 10 -> NULL // List B: 1 -> 7 -> 8 -> 10 -> NULL // Intersection starts at node with value 7 // Question: How many nodes are shared from intersection to end?
Attempts:
2 left
💡 Hint
Count nodes starting from the intersection node to the end.
✗ Incorrect
Nodes shared are 7, 8, and 10, totaling 3 nodes.
