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)value = 5Output5WhyPrimitives are returned as-is.value = {"a": 1, "b": [2, 3, {"c": "x"}]}Output{"a": 1, "b": [2, 3, {"c": "x"}]}WhyAll nested lists/dicts are cloned; primitives are copied by value.Supported types only: None, bool, int, float, str, list, dict (string keys)Reachable container nodes (lists + dicts) <= 2 * 10^5Total primitive elements across all containers <= 10^6Maximum nesting depth <= 10^4If an unsupported type is encountered, raise TypeError