Detect overlap of two linked lists with cycles
Company: Airbnb
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Onsite
Quick Answer: This question evaluates understanding of linked list data structures, cycle detection techniques, pointer/reference identity, and algorithmic time/space complexity analysis within the Coding & Algorithms domain.
Constraints
- 0 <= number of nodes <= 1e5
- Node IDs are integers and uniquely identify nodes (shared nodes use the same ID)
- Each node has exactly one outgoing next pointer (to another node ID) or None
- The structure may contain cycles
- next_map contains all nodes reachable from headA or headB
Examples
Input: ({1: 2, 2: 3, 3: None, 4: 5, 5: None}, 1, 4)
Expected Output: False
Explanation: Two separate acyclic lists: 1->2->3 and 4->5 share no node IDs.
Input: ({1: 2, 2: 3, 3: 6, 6: 7, 7: None, 4: 5, 5: 6}, 1, 4)
Expected Output: True
Explanation: Lists share the tail starting at node 6: A: 1->2->3->6->7 and B: 4->5->6->7.
Input: ({1: 2, 2: 3, 3: 4, 4: 5, 5: 3, 6: 7, 7: 3}, 1, 6)
Expected Output: True
Explanation: Both lists enter the same cycle at node 3. They overlap at node 3 (and all nodes in the cycle).
Input: ({1: 2, 2: 3, 3: 4, 4: 5, 5: 3, 6: 7, 7: 4}, 1, 6)
Expected Output: True
Explanation: List A enters the cycle at node 3, list B enters the same cycle at node 4. Cycles are the same, so they overlap.
Input: ({1: 2, 2: 3, 3: 2, 4: 5, 5: None}, 1, 4)
Expected Output: False
Explanation: List A has a cycle (2<->3), list B is acyclic (4->5). They cannot overlap; otherwise B would also be cyclic.