CODING CHALLENGE · N°60

Data Reconciliation Diff

Medium FDEDataIntegrations

The customer swears the migration worked. You reconcile: compare their source records against what landed in the target, keyed by id, and report exactly what was added, removed, or changed. It’s the diff that ends the "it’s not syncing" argument. Solve it in Python or TypeScript, with hidden tests.

The problem

Implement reconcile(a, b, key). a and b are lists of records (dicts) each identified by the field key. Compare them and return {"added": [...], "removed": [...], "changed": [...]} where: added = key values in b but not a; removed = key values in a but not b; changed = key values in both whose records differ. Each list is the sorted key values.

EXAMPLE 1
Input a = [{"id":1,"v":"a"},{"id":2,"v":"b"}], b = [{"id":2,"v":"B"},{"id":3,"v":"c"}], key = "id"
Output {"added": [3], "removed": [1], "changed": [2]}
id 3 is new, id 1 vanished, id 2’s value changed
CONSTRAINTS
  • Index each list by its key field for O(n) comparison.
  • A record is "changed" if the whole record differs (compare the full dicts), not just one field.
  • Return each category as a sorted list of key values.
SOLVE IT YOURSELF

Your turn — write it

Edit the stub, hit Run (or ⌘/Ctrl + Enter), and watch the hidden tests. Stuck? the hints are right above and Reveal solution is one click away.

YOUR TASK

Implement reconcile(a, b, key): build a lookup of key→record for both sides, then compute the three sets — added, removed, changed — and return them sorted.

HINTS — 4 IDEAS
  1. Build two maps: {record[key]: record} for a and for b.
  2. added = keys in b’s map not in a’s; removed = keys in a’s map not in b’s.
  3. changed = keys in both maps whose records are not equal.
  4. Sort each list of keys before returning.
CPython · WebAssembly

Explore the topic

See this challenge alongside everything else on the same subject — handbooks, system designs, algorithms and tools, in one place.