Data Reconciliation Diff
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.
a = [{"id":1,"v":"a"},{"id":2,"v":"b"}], b = [{"id":2,"v":"B"},{"id":3,"v":"c"}], key = "id"{"added": [3], "removed": [1], "changed": [2]}- Index each list by its
keyfield 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.
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.
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.
- Build two maps:
{record[key]: record}foraand forb. - added = keys in b’s map not in a’s; removed = keys in a’s map not in b’s.
- changed = keys in both maps whose records are not equal.
- Sort each list of keys before returning.
Explore the topic
See this challenge alongside everything else on the same subject — handbooks, system designs, algorithms and tools, in one place.