KatInterview

Diff Two Versions of a Configuration Tree

Trees & GraphsAuto-graded · 24 test cases
HardAuto-gradedTrees & Graphstreeshashingtraversal

Last updated

Tests recursive-free N-ary tree traversal and set-based diffing to count nodes added, removed, or modified between two tree snapshots.

Problem Statement

A software deployment system tracks application configuration as a versioned tree. Each node in the tree represents a configuration entry and carries three fields:

  • "id" — a unique integer identifier, stable across versions
  • "value" — the entry's current setting (a string, number, or None)
  • "children" — a list of child nodes (may be absent on leaf nodes)

You are given two snapshots of this tree: snapshot_a (the earlier version) and snapshot_b (the later version). Write a function that returns the total number of affected nodes when going from snapshot_a to snapshot_b.

A node is affected if any of the following is true:

  1. Modified — the node appears in both snapshots but its value differs.
  2. Added — the node appears in snapshot_b but not in snapshot_a.
  3. Removed — the node appears in snapshot_a but not in snapshot_b.

Two nodes are considered the same node when they share the same id. You may assume that if an id exists in both snapshots, it occupies the same position in the hierarchy — nodes never migrate to a different parent.

Implement:

diff_config_trees(snapshot_a, snapshot_b)

Constraints

  • id values are unique across the entire tree (no two nodes in the same snapshot share an id).
  • The total number of nodes across both snapshots is at most 2 × 10⁵.
  • The tree is N-ary: a node can have any number of children.
  • Either snapshot may be None (representing an empty configuration).
  • A node's "children" key may be omitted entirely; treat it as an empty list.
  • A node's "value" key may be omitted; treat it as None. Two nodes that both have no value are considered equal.
  • Values are compared with Python's != operator. Type differences (e.g., "10" vs 10) count as a modification.
  • Trees can be deeply nested (chains of up to 10⁵ nodes), so solutions relying on Python's default recursion stack may fail.

Examples

Example 1

Input: diff_config_trees({'id': 1, 'value': 'Pizza', 'children': []}, {'id': 1, 'value': 'Pepperoni Pizza', 'children': []})

Output: 1

Example 2

Input: diff_config_trees({'id': 1, 'children': [{'id': 2, 'value': 'Coke', 'children': []}]}, {'id': 1, 'children': []})

Output: 1

Hints

Hint 1

Instead of diffing the trees structurally, think about what information you actually need per node. The stable-id guarantee means you only need to know, for each id, what value it held in each snapshot — the parent-child relationships are irrelevant to the count.

Hint 2

Flatten each snapshot into a dictionary mapping id → value using an explicit stack (not recursion). Then the problem becomes: how many keys in the union of the two dictionaries have different or missing entries?

Hint 3

Iterating over old_dict.keys() | new_dict.keys() visits every id exactly once. For each id, one of four situations holds: present in both with equal values (unchanged), present in both with different values (modified), only in the old dict (removed), or only in the new dict (added). Only the last three contribute to your count.