Detecting and Breaking Circular References in JavaScript Objects

Write a utility function that takes a JavaScript object which may contain circular references (i.e., a nested property somewhere points back to an ancestor object, forming a cycle) and returns a new clean object with all circular references removed or replaced with a sentinel value like '[Circular]'. This is essential before operations like JSON.stringify() which throws on circular structures. Use a WeakSet or visited-node tracking to detect already-seen object references during traversal.

Examples
Input: []
Output:
Hints

Detecting and Breaking Circular References in JavaScript Objects

Write a utility function that takes a JavaScript object which may contain circular references (i.e., a nested property somewhere points back to an ancestor object, forming a cycle) and returns a new clean object with all circular references removed or replaced with a sentinel value like '[Circular]'. This is essential before operations like JSON.stringify() which throws on circular structures. Use a WeakSet or visited-node tracking to detect already-seen object references during traversal.