Skip to solution
hardDSA

How would you write a deep-clone function that correctly handles a self-referencing (circular) object without infinite recursion?

529 views
01

Understand the problem

Question presented to candidate: "Write a deep clone function that correctly copies nested objects and arrays, and does not stack-overflow or infinite-loop if the input contains a cycle — for example an object with a property that points back to itself, or two objects that reference each other. How do you make sure the CLONE'S cycle points back to the new clone, not back to the original object?"

What a strong answer should cover:

  • The core mechanism is a seen map (a WeakMap from original object references to their already-created clones) checked at the very top of the recursive function, before recursing into any of that object's properties.
  • The clone for an object must be registered in the seen map BEFORE recursing into its properties, not after — otherwise a cycle that leads back to the object currently being cloned would not find it in the map yet, and infinite recursion would still happen.
  • When a cycle is hit, the function returns the ALREADY-CREATED (but possibly still-being-populated) clone object reference from the map, not a brand-new one and not the original.
  • A WeakMap (over a plain Map) is the right choice here because it does not prevent the original objects from being garbage collected once cloning is done and the map itself goes out of scope.
  • Special object types — Date, RegExp, Map, Set — need explicit handling, since a naive property-copy loop would not correctly reconstruct them as real instances of those types.
  • Primitives (numbers, strings, booleans, null, undefined, symbols) are returned as-is — cloning is only meaningful for objects and arrays, which are the only things that can participate in a reference cycle in the first place.

Clarifying questions expected:

  • "Does the clone need to preserve property descriptors (getters, non-enumerable flags), or is a plain value copy of own enumerable properties acceptable?" — a real fidelity trade-off, since preserving descriptors means using Object.defineProperty and Reflect.ownKeys instead of a simple for...in-style copy.
  • "Do class instances need to keep their prototype/constructor, or is a plain object clone acceptable even for class instances?" — determines whether Object.create(Object.getPrototypeOf(value)) is needed for the fallback case.

Code / implementation expected: Yes — a full, runnable deep clone using a WeakMap seen-set, plus a real test proving a self-referencing object clones without hanging or throwing, and that the clone's cycle points to the new clone, not the original.

utilitytrickyreal-world
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for JavaScript object-manipulation interview questions. Difficulty: Medium

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. Every result shown below is real, captured output from actually runni

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSA cycle-safe deepClone using a WeakMap seen-set, plus real proof it survives self-reference, mutual, and array cycles without hanging (run directly)
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 146 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track