A fintech risk engine processes millions of events per day and stores intermediate results in a JSON-like in-memory object graph (Python dict/list). For performance, the graph may contain shared sub-objects (aliasing) and, due to caching bugs, can even contain cycles (a container referencing itself). Before running a new model version, the engine must clone this graph so mutations in the new pipeline cannot corrupt the original.
Implement deep_clone(value) that returns a deep copy of a JSON-like value while preserving:
None, bool, int, float, strlist, dict (keys are strings)value (any supported type)Example 1 (cycle):
a = {}
a["self"] = a
b such that b is not a and b["self"] is b.a, it reuses the already-created clone instead of recursing forever.Example 2 (aliasing):
x = {"id": 7}
obj = {"left": x, "right": x}
clone where clone["left"] is clone["right"] and clone["left"] is not x.x must map to the same cloned dict.copy.deepcopy.TypeError.list + dict) ≤ 2 * 10^510^610^4 (so recursion may overflow)