Implement Deep Clone with Circular Reference Handling
Problem Statement
Write a `deepClone(obj)` function that creates a deep duplicate of nested objects, arrays, Dates, and RegExp instances while handling circular references safely without infinite recursion.
Target Complexity
Time:
O(N) where N is total properties and nodes in object graph
Space:
O(N) for recursion call stack and WeakMap cache
Interview Talking Points:
- • Explain why JSON.parse(JSON.stringify(x)) fails on Dates, undefined, Symbols, and throws on circular references.
- • Explain why WeakMap is preferred over Map for circular reference caches (garbage collection friendly).
Solution Editor (JavaScript)
Ready to run
Test Results
Clones nested objects independently
Test #1Handles circular references without crashing
Test #2Correctly clones Date instances
Test #3