Problem, as an interviewer would state it: "Implement an object exposing the same API SHAPE as a real WeakMap (get/set/has/delete) — then explain, honestly, why it can NEVER actually replicate WeakMap's real, defining behavior: automatic garbage collection of unreferenced keys."
Examples:
const wm = new FakeWeakMap();
wm.set(someObject, "data");
// the API works identically -- but the polyfill can NEVER let someObject be garbage collected
Clarifying questions expected:
- Should the polyfill genuinely try to replicate weak references, or is matching the API SHAPE while being honest about the limitation the actual goal?
- Should it reject non-object keys, matching real WeakMap's own documented contract?
- Is there ANY real JS-level way to observe or influence garbage collection directly?
Code / implementation expected: Yes — real, direct proof that the API shape works correctly, PLUS a real, honest demonstration of the actual memory-leak limitation: the polyfill's internal storage is proven to retain a reference even after every other reference to a key is dropped.