Problem, as an interviewer would state it:
"Implement a Reselect-style createSelector(inputSelectors, resultFn) — a derived-state selector that only recomputes when one of its DECLARED input selectors' own output actually changes, ignoring unrelated state changes entirely."
Examples:
const getFilteredItems = createSelector([getItems, getFilter], (items, filter) => items.filter(i => i.includes(filter)));
getFilteredItems(state); // computes
getFilteredItems({ ...state, unrelatedField: 2 }); // returns the SAME cached result, no recompute
Clarifying questions expected:
- Does the memoization compare the FULL state object, or just the specific input selectors' own outputs?
- Should the comparison be a shallow reference check (
===) or a deep equality check? - Does the selector need to support MULTIPLE independent instances, each with their own cache?
Code / implementation expected: Yes — real, direct proof that an unrelated state change does not trigger recomputation (same cached reference returned), while a relevant one does.