Skip to main content

Shallow Copy vs. Deep Copy

Easy

Interview Question: "Can you explain the difference between a shallow copy and a deep copy? If I modify a nested object in a shallow copy, what happens to the original, and how do you handle circular references during deep copies?"

The Quick Answer​

"A shallow copy creates a new outer object, but copies references to any nested objects; both the original and the clone point to the exact same child objects in memory. A deep copy creates a new outer object and recursively clones all nested objects, ensuring the clone shares zero references with the original and can be modified without side-effects."


The ELI5 Analogy: The Briefcase and the House​

Imagine you have a briefcase (the top-level object). Inside is a gold coin (a primitive integer) and a piece of paper with an address to a house (a reference to a nested object).

  • Shallow Copy: You buy a new briefcase and forge a copy of the gold coin. But for the address, you simply copy the same text onto a new slip of paper. Both briefcases point to the exact same physical house. If you go to the house and repaint the walls, whoever opens the original briefcase will also see the painted walls.
  • Deep Copy: You buy a new briefcase, forge the coin, but then hire a construction crew to build an exact twin of the house on a new street. You write the new address in the new briefcase. The two houses are entirely independent.

Technical Comparison​

DimensionShallow CopyDeep Copy
Outer ContainerNew object instance allocated.New object instance allocated.
Nested ObjectsShared references pointing to original memory.Independent clones recursively allocated.
Mutation RiskMutating nested fields corrupts the original.Mutating nested fields is 100% isolated.
PerformanceO(1)O(1) fast, negligible memory.Slower, requires traversing object graph and allocating memory.

The Interview Trap: The Circular Reference Problem​

When asked to implement a custom deepCopy() function, interviewers check if you account for cyclic object graphs:

  • The Problem: If Object A references Object B, and Object B references Object A, a naive recursive deep copy will loop infinitely between them, resulting in a fatal StackOverflowError.
  • The Solution: Maintain a visited identity map (e.g., IdentityHashMap in Java, or a dictionary keyed by id() in Python). Before cloning any reference, check if its memory address already exists in the visited map. If it does, return the cached clone instead of recurring.

Clean Code Example​

Here is how shallow and deep copies are implemented across languages:

class Address {
String city;
Address(String city) { this.city = city; }
}

class User {
String name;
Address address;

// Shallow Copy Constructor: Shares Address reference
User(User other, boolean isDeep) {
this.name = other.name;
if (isDeep) {
// DEEP COPY: Allocates a new Address object
this.address = new Address(other.address.city);
} else {
// SHALLOW COPY: Shares the same Address pointer
this.address = other.address;
}
}
}