Problem, as an interviewer would state it:
"Implement a simplified produce(base, recipe) — like Immer — letting the recipe mutate a 'draft' object naturally, while producing a genuinely NEW immutable result with STRUCTURAL SHARING: unchanged nested parts of the object must be the EXACT SAME reference as the original, not just equal-looking copies."
Examples:
const result = produce(base, draft => { draft.count = 2; });
result.user === base.user; // true -- untouched, structurally shared
result.count === 2 && base.count === 1; // true -- original unmutated
Clarifying questions expected:
- Does "structural sharing" specifically mean REFERENCE equality for untouched parts, not just deep equality?
- If a deeply nested field changes, do all its ANCESTOR objects on the path to it also need new references?
- Should this handle arrays the same way as plain objects?
Code / implementation expected: Yes — real, direct proof that an untouched nested field returns the EXACT SAME reference as the original, while a genuinely modified field (and its ancestors) get new references — this was caught failing on a first attempt and fixed.