Diff Two Flat JSON Objects

Hard ⏱ 18 min 24% acceptance ★★★★☆ 4.2
Write diff(old, new) comparing two flat dicts and returning a dict with three keys: added (keys only in new), removed (keys only in old), and changed (keys in both whose values differ, mapped to (old_value, new_value) tuples).

Examples

Example 1
Input
diff({'a': 1, 'b': 2}, {'b': 3, 'c': 4})
Output
{'added': ['c'], 'removed': ['a'], 'changed': {'b': (2, 3)}}
Explanation

One of each kind of difference.

Constraints

  • added/removed are sorted lists.
  • changed maps to old/new tuples.

Topics

JSONcomparison

Companies

GitHubGitLabBitbucket

Hints

Hint 1

Set algebra on the key views.

Hint 2

Intersection keys need a value comparison.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.