See the difference between copy.copy and copy.deepcopy on nested lists: a shallow copy shares inner objects, a deep copy does not. Proved by tests.
The problem
Write make_copies(original) that returns a tuple (shallow, deep):
shallow— a shallow copy (new outer list, shared inner lists)deep— a deep copy (everything independent)
The test mutates an inner list of the original afterwards and checks which copy sees the change.
Examples
Example 1
Input
probe(make_copies)
Expected output
([1, 2, 99], [1, 2], False, False)
+ 1 hidden test on Submit.
Edge cases to ask about
- Flat vs nested lists
- Copy is not the same object
How the tests call your code
These helpers run before your code. The test inputs above call them.
def probe(make_copies): original = [[1, 2], [3, 4]] shallow, deep = make_copies(original) original[0].append(99) return (shallow[0], deep[0], shallow is original, deep is original)
Hints
0/3How an interviewer scores this
0/9Your code runs in real CPython inside your browser — nothing is sent anywhere. The first run downloads the interpreter (about 6 MB, once). Your code is saved on this device as you type.
Complexity Lab
What does this cost as n grows?
Interviewers score the analysis as much as the code. Commit to an answer first — then check it, and read why.
Pick both to reveal the answer.
From brute force to optimal
The progression an interviewer wants to hear, one step at a time.
| Approach | Time | Space | Idea |
|---|---|---|---|
| copy.copy / copy.deepcopy | O(n) / O(total size) | same | list(x) and x[:] are also shallow copies. |
Walkthrough of the optimal approach (try it yourself first)
A shallow copy (copy.copy, list(x), x[:]) is a new outer list holding the same inner objects — mutate an inner list and both see it. A deep copy (copy.deepcopy) recursively copies every nested object, so the copies are fully independent.
For flat lists of immutable values (ints, strings) the two behave the same.
Complexity: O(n) time, O(n) space. A shallow copy copies the n outer references; a deep copy walks and copies every nested object.
Reveal the reference solution
import copy def make_copies(original): return copy.copy(original), copy.deepcopy(original)
Follow-ups interviewers ask
- How does deepcopy handle cycles?
- Customise copying with deepcopy.
Frequently asked interview questions
Core interview concepts, complexities, and follow-ups scored by hiring teams.
What is the time complexity of Shallow Copy vs Deep Copy in Python?
The optimal solution runs in O(n) time and O(n) auxiliary space. A shallow copy copies the n outer references; a deep copy walks and copies every nested object.
What follow-up questions do interviewers ask about Shallow Copy vs Deep Copy in Python?
How does deepcopy handle cycles? Customise copying with __deepcopy__.
