Problem, as an interviewer would state it: "Given two DOM nodes, find their NEAREST COMMON ANCESTOR — the deepest single node that is an ancestor of (or equal to) BOTH given nodes."
Examples:
findNearestCommonAncestor(nodeC, nodeE); // returns their shared ancestor, e.g. #root
Clarifying questions expected:
- If one node is itself an ancestor of the other, is the answer that ancestor node itself?
- If the same node is passed for both arguments, is the answer that node itself?
- Can this assume the two nodes are genuinely in the SAME document tree, or must it handle the case where they are not?
Code / implementation expected: Yes — real, direct proof against a real jsdom tree, covering both a straightforward two-leaves case and the ancestor/self edge cases.