MediumPro challengePythonJavaScriptTypeScript

Deep Merge

ObjectsRecursion

Implement solve(target, source) that recursively merges source into
target and returns the merged result.

Rules

  • When both values are plain objects, merge recursively.
  • Otherwise, source value overwrites target value.
  • Don't mutate the inputs — return a new object.

Example

solve({ a: 1, b: { c: 2, d: 3 } }, { b: { c: 99, e: 4 }, f: 5 })
// → { a: 1, b: { c: 99, d: 3, e: 4 }, f: 5 }

Sample tests

Test #1Standard nested merge
Input: [{"a":1,"b":{"c":2,"d":3}},{"b":{"c":99,"e":4},"f":5}]
Output: {"a":1,"b":{"c":99,"d":3,"e":4},"f":5}
Test #2Empty target
Input: [{},{"a":1}]
Output: {"a":1}
Test #3Empty source
Input: [{"a":1},{}]
Output: {"a":1}
Test #4Arrays are overwritten, not merged
Input: [{"a":[1,2]},{"a":[3,4]}]
Output: {"a":[3,4]}
Test #5Three levels deep
Input: [{"x":{"y":{"z":1}}},{"x":{"y":{"w":2}}}]
Output: {"x":{"y":{"w":2,"z":1}}}