Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Deep Clone Utility Function

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Deep Clone Utility Function. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Cashfree Checkout configuration objects can contain nested dictionaries and lists. Implement a utility that creates a fully independent deep clone, so changing the clone never changes the original configuration.

Your function must also preserve reference relationships. If two properties in the input point to the same nested object, the corresponding properties in the clone must point to the same cloned object. Cyclic dictionaries or lists must not cause infinite recursion.

Formal Specification

Implement deep_clone(value), where value is a JSON-like Python value composed of dictionaries with string keys, lists, and primitive values: None, booleans, integers, floats, and strings. Return a cloned value with the same structure and values.

Primitive values may be reused. Every dictionary and list must be newly allocated. The input must not be modified. Use an identity-based memoization map to handle repeated references and cycles.

Constraints

  • The input contains only dictionaries, lists, and None, booleans, integers, floats, or strings.
  • Dictionary keys are strings.
  • The total number of dictionary and list containers is at most 10^4.
  • Nesting depth is at most 10^3.
  • Repeated references and cycles may occur.

Function Signature

def deep_clone(value):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output