Skip to the page

DSA for frontend · Build it

Find the matching node in an identical DOM tree (LeetCode 1379)

The interviewer asks

You have two identical DOM trees, A and B, and a node inside A. Return the corresponding node in B.

Then

Follow-ups the interviewer asks next

  1. No parent pointers.
  2. Use a TreeWalker.
  3. The trees differ slightly.
  4. Complexity in terms of depth and breadth.

The answer

What a strong answer covers

  • walk up from the node to the root, recording its index in parent.children at each level; replay that path down in B; O(depth × branching) time with indexOf, O(depth) space; without parent pointers, traverse both trees in lockstep until you reach the node, O(n).

The red pen

Common traps

Avoid:matching by id or textContent (not unique); mixing childNodes and children (text nodes shift indices).

Practise this out loud

Say your answer out loud to an AI interviewer that follows up, and get a score for it.

Practise by voice

Practise questions like this out loud

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview