nextRound
TechnologiesCoding ProblemsBookmarksLearning PathsLogin
nextRound
TechnologiesCoding ProblemsBookmarksLearning PathsLogin
nextRound

AI-powered interview preparation platform. Practice with curated questions, mock interviews, and personalized learning paths to crack your dream tech interview.

Quick Links

  • Technologies
  • Mock Interviews
  • Saved Questions
  • Pricing

Company

  • About Us
  • Contact Us

Legal

  • Privacy Policy
  • Terms of Use

© 2026 nextRound. All rights reserved.

Questions
5 of 12
1What is an Interpreted language?
2Is Python a compiled language or an interpreted language?
3What is the difference between `/` and `//` in Python?
4Why can `a = 256; b = 256; a is b` return `True`, but `a = 257; b = 257; a is b` return `False` in some Python implementations?
5What is the difference between a shallow copy and a deep copy? When would each cause bugs?
6What causes a reference cycle, and how does Python's garbage collector handle it?
7What is the difference between `is` and `==`?
8How does Python evaluate chained comparisons like `1 < x < 10`?
9What are Python's built-in data types, and how are they categorized (mutable vs. immutable)?
10How does Python manage memory for objects internally (reference counting and the object model)?
11Why does Python not require explicit variable declarations, and how does dynamic typing affect variable assignment internally?
12Why can floating-point arithmetic in Python produce results like `0.1 + 0.2 != 0.3`? How would you correctly compare floats?
PythonPython
Basics
Control Flow and Functions
Data Structures
Comprehensions & Functional Programming
Iterators, Generators & Decorators
Object-Oriented Programming
Exception Handling & Debugging
Concurrency & Parallelism
Performance & Optimization
Testing
Security
Modules, Packaging & Environment
Type Hinting & Modern Python
System Design & Architecture with Python
Best Practices & Design Patterns
Edge Cases & Tricky Interview Questions
05 / 12

What is the difference between a shallow copy and a deep copy? When would each cause bugs?

Difficulty: 5/10
Variables, Data Types & Memory Model, Shallow vs Deep Copy, Shared References in Nested Structures

Shallow vs deep copy and shared references

A shallow copy creates a new outer container but fills it with references to the same inner objects. A deep copy recursively creates new copies of everything reachable, so the result shares no mutable state with the original. In the standard library, copy.copy gives the first and copy.deepcopy the second. list(x), x[:], x.copy(), dict(x) and set(x) are all shallow.

javascript

When shallow copies bite: any nested mutable structure (list of lists, dict of lists, objects holding collections). Mutating the 'copy' silently mutates the original, and the symptom often appears far from the cause, such as config dicts altered per request or the [[0]*n]*m grid bug. When deep copies bite: they are expensive (time and memory proportional to the whole object graph), they can copy things that were meant to be shared (a logger, cache, connection pool, singleton), they fail or misbehave on objects that cannot be copied (locks, sockets, file handles, some C extension objects), and they can break identity assumptions elsewhere in your program.

How deepcopy works and how to customize it: it keeps a memo dict mapping id(original) to the new object, which is how it handles cycles and preserves shared references inside the copied graph (two attributes pointing to one object still point to one object in the copy). You can control behaviour with copy and deepcopy (for example, copy the data but keep a reference to the shared connection).

Alternatives I reach for in real systems: avoid the problem with immutable data (tuples, frozen dataclasses, dataclasses.replace), copy only what you will mutate (copy-on-write at the path you change), or use persistent data structures. Common mistake in interviews: saying 'copy() copies the object' without stating what is shared. Always say what is shared, then give the bug it causes.

Scenario Questions

0-2 years experience

  1. 1You write b = a[:] for a list of lists and then change b[0][0]; why does a change too?
  2. 2Why is [[0] * 3] * 3 wrong for creating a 3x3 grid, and what should you write instead?

2-5 years experience

  1. 1A function mutates the settings dict passed to it and callers complain their dicts changed. How do you fix it, and when is copy.copy enough versus deepcopy?
  2. 2deepcopy on an object that holds a lock or database connection raises an error or behaves oddly. Why, and how would you use __copy__ or __deepcopy__ to handle it?

5-8 years experience

  1. 1A web service deep-copies a large config for every request and shows up as a CPU hotspot. What alternatives would you consider?
  2. 2Explain how deepcopy treats two attributes that point to the same sub-object, and why the memo dict is essential.

8+ years experience

  1. 1Design an undo/redo or game-state system over a large in-memory structure. Compare full copies with structural sharing / persistent data structures and explain the trade-offs.
  2. 2You send large nested objects to worker processes. Compare deepcopy, pickling, fork's copy-on-write and shared memory, and explain pitfalls such as reference-count writes defeating copy-on-write.

Follow-up Questions

  • How does copy.deepcopy handle circular references and shared sub-objects?
  • How do you customise copying for a class that holds a database connection?
Sharethis question

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.