This is a premium problem. We're working on making it available for free soon.
Explore Free ProblemsUse these hints if you're stuck. Try solving on your own first.
Javascript has the concept of Proxy. That concept is critical to this problem.
You you need to keep track of which values in the JSON were overwritten with new values.
Somehow, keep a tree structure that exists parallel to the original object. This will keep track of all the edits.
When the mutator function is done. Return a clone with those mutations applied. It will be inefficient to clone the entire object so only clone the minimum number of nodes.
Solutions for this premium problem will be available for free soon.
Browse Free ProblemsWatch expert explanations and walkthroughs
Jot down your thoughts, approach, and key learnings
While the exact problem may not always appear, the concept of immutable updates and recursive object manipulation is common in frontend and JavaScript-heavy interviews. Companies often test candidates on safe state updates and understanding of persistent data patterns.
The optimal approach is to recursively process the update specification and apply commands such as $set, $merge, or $push. Instead of mutating the original object, create shallow copies only for the parts that change. This ensures predictable immutable updates and efficient memory usage.
The problem primarily uses JavaScript objects and arrays combined with recursive traversal. A helper function that interprets command keys and creates shallow copies of objects or arrays is the most practical structure for implementing the solution.
Immutability prevents unintended side effects by ensuring the original data structure remains unchanged. This is especially important in functional programming patterns and UI frameworks like React, where predictable state updates are critical.