MediumPythonJavaScriptTypeScript

Deep Clone

ObjectsFunctions

Implement solve(value) that returns a fully independent deep clone of any
JSON-shaped value (primitives, arrays and plain objects, possibly nested).

Requirements

  • Primitives (numbers, strings, booleans, null) are returned as-is.
  • Arrays and objects are reconstructed top-to-bottom — mutating the result

must never affect the original input.

  • The clone must be structurally equal to the input.

Out of scope
You don't need to handle Date, Map, Set, RegExp, circular
references or class instances. Focus on plain JSON shapes.

Forbidden shortcut
Don't use JSON.parse(JSON.stringify(x)). The point is to demonstrate the
recursion mechanics yourself.

Sample tests

Test #1Primitive returned as-is
Input: [42]
Output: 42
Test #2null is a valid clonable value
Input: [null]
Output: null
Test #3Nested object with an inner array
Input: [{"a":1,"b":{"c":2,"d":[3,4]}}]
Output: {"a":1,"b":{"c":2,"d":[3,4]}}
Test #4Deeply nested arrays
Input: [[1,[2,[3,[4,[5]]]]]]
Output: [1,[2,[3,[4,[5]]]]]
Test #5Array of objects with nested arrays
Input: [{"users":[{"id":1,"tags":["a"]},{"id":2,"tags":["b","c"]}]}]
Output: {"users":[{"id":1,"tags":["a"]},{"id":2,"tags":["b","c"]}]}