DSA for frontend · Build it
Find the matching node in an identical DOM tree (LeetCode 1379)
Practise this out loud
Say your answer out loud to an AI interviewer that follows up, and get a score for it.
Practise by voiceThe 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
- No parent pointers.
- Use a TreeWalker.
- The trees differ slightly.
- 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 voicePractise 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.