MediumPro challengeJavaScriptTypeScript

List Reconciliation by Key

ReactReconciliationAlgorithms

React reconciles lists by matching keys. Implement solve(prev, next)
that returns operations to transform prev into next:

  • { type: 'keep', key } — node exists in both.
  • { type: 'remove', key } — in prev only.
  • { type: 'add', key } — in next only.

Order: removes first (in prev order), then keeps and adds (in next order).

Sample tests

Test #1Append c
Input: [["a","b"],["a","b","c"]]
Output: [{"key":"a","type":"keep"},{"key":"b","type":"keep"},{"key":"c","type":"add"}]
Test #2Remove a
Input: [["a","b"],["b"]]
Output: [{"key":"a","type":"remove"},{"key":"b","type":"keep"}]
Test #3Add to empty
Input: [[],["x"]]
Output: [{"key":"x","type":"add"}]
Test #4Reorder, all keep
Input: [["a","b","c"],["c","b","a"]]
Output: [{"key":"c","type":"keep"},{"key":"b","type":"keep"},{"key":"a","type":"keep"}]
Test #5Empty result
Input: [["a"],[]]
Output: [{"key":"a","type":"remove"}]