Find the Intersection Node of Two Linked Lists
Reported by candidates from Tesla's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Tesla reported this one in October 2022, and it looks friendlier than it is. You're handed a node table, two heads, and asked for the first shared node by identity, not by value. With up to 10^5 nodes, a nested scan comparing every node in A against every node in B is the move that gets you a timeout. The real answer is linear time and constant extra space. If you've got an OA invite and 48 hours, this is a pattern worth locking in. StealthCoder sits invisibly on your screen as a safety net if your mind goes blank mid-assessment.
The problem
Two singly linked lists are represented by one shared node table. Row nodes[i] describes node identity i as [value, nextIndex], where nextIndex is another node index or -1. The two lists start at headA and headB. Because both heads use the same node table, reaching the same node index means the lists intersect by identity. Return the index of the first shared node, or -1 if the lists do not intersect. Do not modify nodes. Function findIntersectionNodeIndex(nodes: int[][], headA: int, headB: int) → int Examples Example 1 nodes = [[4,1],[1,2],[8,3],[4,4],[5,-1],[5,6],[6,2]] headA = 0 headB = 5 return = 2 List A is 0 -> 1 -> 2 -> 3 -> 4, and list B is 5 -> 6 -> 2 -> 3 -> 4. Their first shared node is index 2. Example 2 nodes = [[1,1],[2,-1],[3,3],[4,-1]] headA = 0 headB = 2 return = -1 The paths 0 -> 1 and 2 -> 3 share no node identity. Example 3 nodes = [[7,1],[8,2],[9,-1]] headA = 0 headB = 1 return = 1 List B begins inside list A, so its head at index 1 is the first shared node. Constraints 0 <= nodes.length <= 10^5. Every row of nodes has exactly two integers: [value, nextIndex]. Each nextIndex is -1 or a valid node index. headA and headB are -1 or valid node indices. The chains reachable from both heads are acyclic. -10^9 <= nodes[i][0] <= 10^9.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is the two-pointer switch. Start pointer a at headA and pointer b at headB. Each step, move to nextIndex. When a pointer hits -1, redirect it to the other list's head. Both pointers travel lenA + lenB steps total, so they line up at the first shared index, or both hit -1 together if there's no intersection. No hash set needed, though a visited set is a fine fallback at O(n) memory. Pitfalls: compare indices, never values, since Example 1 has duplicate values like 4 and 5. Handle headA or headB equal to -1 up front. Redirect on reaching -1 only once per pointer, or you'll loop forever on disjoint lists. Example 3 shows headB can start inside A, so don't assume heads differ. If you blank on the switch logic during the live OA, StealthCoder gives you the pointer-swap solution right then.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Find the Intersection Node of Two Linked Lists cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as intersection of two linked lists. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Tesla's OA.
Tesla reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Find the Intersection Node of Two Linked Lists FAQ
How hard is the Tesla intersection node problem really?+
Easy to medium. The brute force is obvious and the optimal is about ten lines. The difficulty is the index-based node table instead of real node objects, which trips people up on comparisons and the -1 sentinel.
What's the trick to avoid O(n*m) time?+
Use two pointers that swap to the other list's head after reaching the end. Both cover equal total distance, so they meet at the intersection or both reach -1 together. Alternatively, store visited indices from A in a set and walk B.
Do I compare values or indices?+
Indices. The problem says intersection is by node identity. Values repeat, like the two 4s and two 5s in Example 1, so comparing values would return wrong answers.
What edge cases should I test before submitting?+
Empty nodes with both heads -1, one head -1, headB starting inside A like Example 3, identical heads, and fully disjoint lists like Example 2. Those cover the pointer-switch termination bugs.
How do I prepare for this in 48 hours?+
Write the two-pointer swap from scratch twice, then the hash set version. Trace Example 1 by hand with the pointer switch. Once you can explain why both pointers travel equal distance, you've got it.