Question presented to candidate: "You get the heads of two singly linked lists. If they merge into one shared tail, return the first shared node; otherwise return None. The lists have no cycles and must not be changed. Can you do it in O(1) extra memory? Please write it in Python."
What a strong answer should cover:
- Intersection means the same node object, not equal values; the shared part is a common tail (Y shape).
- A hash-set baseline in O(m + n) time and O(m) space.
- An O(1)-space answer: align by length difference, or the switch-heads two pointers that both walk a + c + b nodes.
- Why the switch-heads loop ends with no intersection: both pointers become None on the same step.
- Python specifics: compare with
is, write the switch aspa = pa.next if pa else head_b, and remember that a plain class hashes by identity soset()holds nodes, not values.
Clarifying questions expected:
- "Same node, or equal values?"
- "Can the lists contain cycles, and may I modify them?"
Code / implementation expected: Yes โ get_intersection(head_a, head_b) in Python, O(1) space.