Frontend-flavoured problems · mid
DOM tree problems
DOM questions are tree questions with DOM details: walk element.children (not childNodes) with DFS or BFS, match classes by token, and for 'find the same node in a mirrored tree' record the child-index path up to the root and replay it down the other tree.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- children has only elements; childNodes also has text and comment nodes, which shift indices.
- Corresponding node in an identical tree (Meta, BigFrontEnd #19): walk up with parentElement recording indexOf in parent.children, then walk down tree B with the reversed path. O(depth × branching) time; or walk both trees in lockstep, O(n).
- getElementsByClassName from scratch: DFS in document order; split the query by whitespace; match with classList.contains for each class (token match: 'btn' must not match 'btn-primary'); decide whether root itself counts.
- getElementsByStyle: compare getComputedStyle(el).getPropertyValue(prop) with a normalised target (set the value on a temporary element and read it back, so 'red' becomes 'rgb(255, 0, 0)').
- Level-order (BFS) gives 'next right sibling on the same level', 'tree height' and 'nodes at depth k'. Browsers also have TreeWalker and NodeIterator for filtered walks.
- LCA in the DOM: walk up from one node collecting ancestors in a Set, then walk up from the other; or use node.contains().
- Iterative traversal (stack) avoids recursion limits on huge DOMs.
The red pen
Common traps: mistake → fix
Mistake: Match by id or textContent
Fix: not unique; match by position.
Mistake: className.includes('x')
Fix: matches 'xy'; use token matching.
Mistake: Use childNodes and index
Fix: text nodes shift the indices between trees.
Easy to hard
Questions interviewers ask, easy to hard
- Find the matching node in an identical DOM tree.
- Do it without recursion. What is the time cost of each approach?
- Implement getElementsByClassName with multiple classes.
- Find the next element to the right on the same level.
- Implement getElementsByStyle; why compare computed values?
Practise
Practise with these questions
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.