Problem, as an interviewer would state it:
"Implement setIn(obj, path, value) — setting a deeply nested value at path and returning a genuinely NEW object, while leaving the original COMPLETELY untouched, and correctly SHARING (not copying) any sibling data that was not actually on the changed path — matching the real, well-known Immutable.js setIn convention."
Examples:
const original = { user: { name: "Ada", address: { city: "London" } } };
setIn(original, "user.address.city", "Paris");
// { user: { name: "Ada", address: { city: "Paris" } } }
// original is completely unchanged
Clarifying questions expected:
- Should every object ALONG the changed path be a genuinely new reference, while sibling data NOT on that path is structurally SHARED (same reference) rather than deep-copied?
- Does this need to correctly CREATE intermediate objects if the path does not fully exist yet in the original?
- Should the path be accepted as a dot-separated string, an array of keys, or both?
Code / implementation expected: Yes — real, direct proof the original is completely untouched, that every object along the changed path is a new reference while unrelated sibling data is structurally shared, and that missing intermediate path segments are correctly created.