Challenge - 5 Problems
Intersection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Intersection Point Detection
What is the output of the following C++ code that finds the intersection point of two linked lists?
DSA C++
struct Node {
int data;
Node* next;
Node(int x) : data(x), next(nullptr) {}
};
Node* getIntersectionNode(Node* headA, Node* headB) {
Node *a = headA, *b = headB;
while (a != b) {
a = a ? a->next : headB;
b = b ? b->next : headA;
}
return a;
}
int main() {
Node* common = new Node(8);
common->next = new Node(10);
Node* headA = new Node(3);
headA->next = new Node(7);
headA->next->next = common;
Node* headB = new Node(99);
headB->next = new Node(1);
headB->next->next = common;
Node* intersection = getIntersectionNode(headA, headB);
if (intersection) {
std::cout << intersection->data << std::endl;
} else {
std::cout << "No intersection" << std::endl;
}
return 0;
}Attempts:
2 left
💡 Hint
Trace the pointers moving through both lists until they meet.
✗ Incorrect
The code uses two pointers starting at heads of each list. They traverse and switch lists when reaching the end. They meet at the intersection node with value 8.
🧠 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
Think about the total distance each pointer travels.
✗ Incorrect
Each pointer traverses both lists fully, so if there is an intersection, they align at the intersection node after equal steps.
🔧 Debug
advancedIdentify the Bug in Intersection Code
What error will the following code produce when trying to find the intersection point of two linked lists?
DSA C++
struct Node {
int data;
Node* next;
Node(int x) : data(x), next(nullptr) {}
};
Node* getIntersectionNode(Node* headA, Node* headB) {
Node *a = headA, *b = headB;
while (a != b) {
a = a->next ? a->next : headB;
b = b->next ? b->next : headA;
}
return a;
}Attempts:
2 left
💡 Hint
Check what happens when a or b is nullptr before accessing next.
✗ Incorrect
The code tries to access a->next without checking if a is nullptr, causing a crash.
❓ Predict Output
advancedOutput of Intersection Detection with No Intersection
What is the output of this code when two linked lists do not intersect?
DSA C++
struct Node {
int data;
Node* next;
Node(int x) : data(x), next(nullptr) {}
};
Node* getIntersectionNode(Node* headA, Node* headB) {
Node *a = headA, *b = headB;
while (a != b) {
a = a ? a->next : headB;
b = b ? b->next : headA;
}
return a;
}
int main() {
Node* headA = new Node(1);
headA->next = new Node(2);
headA->next->next = new Node(3);
Node* headB = new Node(4);
headB->next = new Node(5);
headB->next->next = new Node(6);
Node* intersection = getIntersectionNode(headA, headB);
if (intersection) {
std::cout << intersection->data << std::endl;
} else {
std::cout << "No intersection" << std::endl;
}
return 0;
}Attempts:
2 left
💡 Hint
If no intersection, pointers meet at nullptr.
✗ Incorrect
Both pointers eventually become nullptr at the same time, so the function returns nullptr and prints 'No intersection'.
🚀 Application
expertNumber of Nodes in Intersection Part
Given two singly linked lists that intersect, how can you find the number of nodes in the intersection part (shared tail)?
Attempts:
2 left
💡 Hint
Once you find the intersection node, count nodes from there to the end.
✗ Incorrect
The intersection part is the shared tail starting from the intersection node. Counting nodes from that node to the end gives the length.
