Diff 2 versions of a product catalog

Python data engineering interview problem. Difficulty: advanced. Pattern: Dictionaries. About 20 minutes. Part of the Pro drill bank.

A product catalog is published as 2 versions. Merchandising wants what was added, removed, or changed so they can write a changelog.

When a product catalog is updated, a release manager wants to see which products were added, which were removed and which changed. old and new are dictionaries from product key to details. Return a dictionary with three sorted lists: added (keys only in new), removed (keys only in old) and changed (keys in both whose details are different). Write catalog_diff(old, new). If product A changes price from 1 to 2 and product B is new, the result is {'added': ['B'], 'removed': [], 'changed': ['A']}. Two empty catalogs give three empty lists. The starter file already has the function name and its print lines. Keep them and fill in the body.

Requirements

  • added: keys that are only in new.
  • removed: keys that are only in old.
  • changed: keys in both with different values.
  • Sort each list.

Constraints

  • Both inputs are dictionaries.
  • The result has exactly the keys added, removed and changed.

Examples

Input: catalog_diff({'A': {'name': 'a', 'price': 1}}, {'A': {'name': 'a', 'price': 2}, 'B': {'name': 'b', 'price': 3}}) Output: {'added': ['B'], 'removed': [], 'changed': ['A']} This input follows the stated rules and produces this output.

Topics: lakebench, python, data-transformation.

More Python interview questions · All interview problems · Learn data engineering

advanced

Diff 2 versions of a product catalog

Interview-style drill: Given two snapshots of a product catalog, report which products were added, removed, or changed.

Diff 2 versions of a product catalog.