Skip to content
React logic · Senior · about 25 minutes

Reconcile a keyed list

Implement reconcileKeys(oldKeys, newKeys), a small model of how React updates a keyed list. Both are arrays of unique strings. Return operations in this order. First a { type: 'remove', key } for every old key that is gone, in old order. Then walk newKeys from left to right with lastIndex = -1: a key that was not in oldKeys gives { type: 'insert', key, index }; for a key that existed, let oldIndex be its old position: when oldIndex < lastIndex it gives { type: 'move', key, index }, otherwise it gives nothing and lastIndex becomes oldIndex. index is the position in newKeys.

Start from

function reconcileKeys(oldKeys, newKeys) {
  // Return the remove, insert and move operations.
}

Hints and solution

Solve it in the app: its tests run without a year.

The hints, the solution and what an interviewer looks for opens with a year in the app. What a year includes