Problem, as an interviewer would state it:
"Write deepClone(obj) that correctly clones plain objects, arrays, Dates, RegExps, Maps and Sets, and handles circular references. Discuss edge cases, runtime, and alternatives."
Examples:
const a = { d: new Date(0), m: new Map([["k", { deep: 1 }]]), list: [1, [2]] };
a.self = a;
const b = deepClone(a);
b.self === b; // true (the cycle points at the copy, not the original)
b.m !== a.m; // true (nothing is shared with the original)
Clarifying questions expected:
- Should class instances keep their prototype, and are functions copied or shared?
- Are Map keys cloned as well as values, and what about symbol keys and non-enumerable properties?
- Why not
structuredClone, and which kinds (Errors, typed arrays) are out of scope?
Code / implementation expected: Yes — real code, checked against structuredClone and on circular and shared structures.