Count changed nodes in N-ary trees
Company: DoorDash
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Overview: This question evaluates understanding of N-ary tree data structures, structural comparison of hierarchical versions, and the ability to reason about time and space complexity when detecting added, deleted, value-changed, or reparented nodes.
Constraints
- 0 <= len(old_tree), len(new_tree) <= 200000
- Sum of lengths of all children lists in each tree <= 200000
- Keys are unique within each tree and identify nodes across versions
- Values are 32-bit signed integers
- Each input describes a valid rooted tree (acyclic, each non-root has exactly one parent)
Hints
- Map each key to its value and its parent key (None for the root) in both trees.
- Build parent maps by scanning children lists; you do not need to traverse the trees.
- Compare over the union of keys from both trees to count additions, deletions, value changes, and parent (move) changes.
- Treat the root's parent as None when comparing parent keys.