As asked
Explain why React's list diffing algorithm is O(n) rather than the theoretical O(n^3) tree diff, and what role the key prop plays in that. What actually goes wrong at runtime when you use an array index as a key for a reorderable list?
Sample answer outline
Covers the two heuristics React uses: same type at same position stays, and keys short-circuit cross-position matching. Explains that index-as-key causes wrong component state to be kept alive (e.g., controlled input values, animation state) when items are inserted or reordered because React maps the same key to a different data item.
Expect these follow-ups
- When is it actually safe to use an array index as a key?
- How does key affect component lifecycle, specifically when does a component unmount vs. receive new props?