KatInterview

Diff Two Versions of a Keyed Hierarchy

Trees & GraphsAuto-graded · 18 test cases
MediumAuto-gradedTrees & Graphstreehash-mapdfs

Last updated

Compare two keyed N-ary trees given as nested dicts and count nodes that were added, removed, given a new value, or attached to a different parent.

You are given two snapshots of a hierarchy: an earlier one (before) and a later one (after). Each snapshot is either None (an empty hierarchy) or a nested dictionary describing a rooted tree with any number of children per node.

Every node is a dictionary with these fields:

  • "key": a string that identifies the node.
  • "value": an integer payload.
  • "children": a list of node dictionaries (possibly empty). The field may be missing, in which case the node has no children.

Within a single snapshot, all keys are distinct, so a key identifies one node across both snapshots. The root of a snapshot has no parent (treat its parent as None).

Task

Return the number of nodes that are changed in the later snapshot compared with the earlier one. A key counts as changed if any of the following holds:

  1. Removed: the key appears in before but not in after.
  2. Added: the key appears in after but not in before.
  3. Value differs: the key appears in both, but its "value" is different.
  4. Reparented: the key appears in both with equal values, but its parent's key differs between the snapshots. A root has parent None, so a node that was the root in one snapshot and has a parent key in the other is reparented.

Each key is counted at most once, even if it satisfies several conditions (for example, both a new value and a new parent). Only the node's own parent matters: when a node moves, its descendants whose parent keys are unchanged are not counted for that reason. The order of children in a list has no effect on the result.

Constraints

  • Either input may be None; if both are None the answer is 0.
  • Keys are unique within each snapshot.
  • Values are integers; keys are strings.
  • The number of nodes in each tree can be large (up to roughly 10^5), and a tree may be very deep, so avoid relying on deep recursion.
  • Aim for time linear in the total number of nodes.

Examples

Example 1

Input: count_changed_nodes({'key': 'root', 'value': 1, 'children': [{'key': 'a', 'value': 2, 'children': []}]}, {'key': 'root', 'value': 1, 'children': [{'key': 'a', 'value': 3, 'children': []}, {'key': 'b', 'value': 4, 'children': []}]})

Output: 2

Example 2

Input: count_changed_nodes({'key': 'root', 'value': 1, 'children': [{'key': 'a', 'value': 2, 'children': []}, {'key': 'b', 'value': 4, 'children': []}]}, {'key': 'root', 'value': 1, 'children': [{'key': 'b', 'value': 4, 'children': []}, {'key': 'a', 'value': 2, 'children': []}]})

Output: 0

Example 3

Input: count_changed_nodes({'key': 'root', 'value': 1, 'children': [{'key': 'a', 'value': 2, 'children': []}]}, {'key': 'root', 'value': 1, 'children': [{'key': 'b', 'value': 2, 'children': [{'key': 'a', 'value': 2, 'children': []}]}]})

Output: 2

Hints

Hint 1

Whatever the tree shape, each node's relevance to the answer comes down to three facts: whether it exists, its value, and its parent's key.

Hint 2

Flatten each snapshot into a dictionary from key to (value, parent_key) using an iterative traversal with an explicit stack.

Hint 3

Walk over the union of keys from both dictionaries once: a key missing from either side is changed; otherwise it is changed exactly when the two (value, parent_key) tuples differ.