Hone logo
Hone
Problems

Deep Clone an Object

You are tasked with implementing a function that can create a perfect, independent copy of a given object. This is known as a "deep clone." A deep clone is essential when you need to modify a complex data structure without affecting the original object, which is a common requirement in many programming scenarios, especially when dealing with nested objects or arrays.

Problem Description

The goal is to write a function deepClone that accepts a single argument, obj, which can be any JavaScript-like data structure (including primitives, objects, and arrays). The function should return a completely new object that is an exact replica of the input object, with no shared references.

Key Requirements:

  • Handles Primitives: Numbers, strings, booleans, null, and undefined should be copied by value.
  • Handles Objects: Plain JavaScript objects (created with {} or new Object()) should be recursively cloned. All properties, including their values, should be copied.
  • Handles Arrays: Arrays should be recursively cloned. All elements, including their values, should be copied.
  • Handles Nested Structures: The cloning process must correctly handle arbitrarily nested objects and arrays.
  • No Shared References: The cloned object and its nested components must be distinct from the original object and its nested components. Modifying the clone should not affect the original.

Expected Behavior:

  • If the input is a primitive type, return that primitive.
  • If the input is an object, return a new object with all its properties (and their cloned values) copied.
  • If the input is an array, return a new array with all its elements (and their cloned values) copied.

Edge Cases to Consider:

  • null: Should be treated as a primitive and returned directly.
  • undefined: Should be treated as a primitive and returned directly.
  • Circular References: While not explicitly required for this initial challenge, be aware that a naive recursive approach can lead to infinite loops if objects contain circular references. For this problem, assume no circular references exist.

Examples

Example 1:

Input: 123
Output: 123
Explanation: Primitive types are returned as is.

Example 2:

Input: { name: "Alice", age: 30, address: { street: "123 Main St", city: "Anytown" } }
Output: { name: "Alice", age: 30, address: { street: "123 Main St", city: "Anytown" } }
Explanation: The output is a new object. If you were to modify the 'address' property of the output, the original object's 'address' would remain unchanged.

Example 3:

Input: [1, { a: 2 }, [3, 4]]
Output: [1, { a: 2 }, [3, 4]]
Explanation: The output is a new array. The nested object and array are also new, independent copies.

Example 4:

Input: null
Output: null
Explanation: Null is treated as a primitive.

Constraints

  • The input obj will be a valid data structure.
  • The input will not contain circular references.
  • The depth of nested objects and arrays is not explicitly limited, but your solution should be efficient enough for typical use cases.
  • Focus on cloning plain JavaScript objects and arrays. Do not worry about cloning Dates, Regexps, Maps, Sets, or other complex built-in object types for this challenge.

Notes

Consider how to distinguish between an object and an array. A common approach is to check the constructor or use Array.isArray(). Think about the base cases for your recursion. How will you ensure that primitive values are handled correctly without unnecessary recursion?

Loading editor...
plaintext