Katkat

Implement deep clone for complex objects

AppleSoftware Engineer · Technical Screen

Last updated

This question evaluates mastery of deep object cloning, reference and memory management, and knowledge of language runtime details such as prototypes, property descriptors, symbol-keyed and non-enumerable properties, and built-in types like Maps, Sets, Dates, RegExps, and typed arrays.

Implement a deep-clone function for nested objects in JavaScript (or a language of your choice) without using JSON serialization or third-party libraries. Requirements:

  • Correctly copy primitives, plain objects, arrays, Maps, Sets, Dates, and RegExps.
  • Preserve object prototypes and property descriptors (writable, enumerable, configurable) when possible.
  • Handle circular references and shared substructures so identical references remain shared after cloning.
  • Support symbol keys and non-enumerable properties.
  • Do not clone functions; keep function references as-is.
  • Discuss time and space complexity, recursion vs. iteration, and stack-safety for very deep graphs.
  • Provide tests for edge cases such as self-referential objects, sparse arrays, NaN/Infinity/-0, typed arrays, and large object graphs.