Reviewed by Aditya Kumar · Last reviewed 2026-03-24
Python's garbage collector (GC) primarily uses reference counting to manage memory, immediately deallocating objects when their reference count drops to zero. To handle circular references , which…
This easy-level Python/Coding question appears frequently in data engineering interviews at companies like NAB. While less common, it tests deeper understanding that distinguishes strong candidates. Mastering the underlying concepts (python) will help you answer variations of this question confidently.
Start by clearly defining the core concept being asked about. Interviewers want to see that you understand the fundamentals before diving into implementation details. Structure your answer with a definition, then explain the practical application with a concise example. The expert answer includes a code example that demonstrates the implementation pattern.
Python's garbage collector (GC) primarily uses reference counting to manage memory, immediately deallocating objects when their reference count drops to zero. To handle circular references, which reference counting cannot resolve, it employs a generational garbage collector.
del statement). When an object's reference count reaches zero, its memory is immediately reclaimed. This method is efficient and deterministic for most objects, providing prompt memory release.Consider two objects, a and b, referencing each other:
class Node:
def __init__(self):
self.ref = None
a = Node()
b = Node()
a.ref = b
b.ref = a
# Even if 'a' and 'b' are no longer referenced externally,
# their internal ref counts remain 1, preventing ref counting from cleaning them.
In CPython, reference counting is the primary mechanism, offering immediate deallocation. The generational collector acts as a fallback for the less common, but critical, scenario of circular references.
In the interview, also mention that while Python's GC is mostly automatic, the built-in gc module allows manual control (gc.collect()) and inspection (gc.get_count(), gc.get_threshold()). In production, focus on designing code to avoid circular references for large objects rather than frequent manual GC tuning, as it can introduce unpredictable performance overhead.
Pro-Move: weakref for caches. Red Flag: gc.disable() in production.
Some links below are affiliate links. If you buy through them we may earn a small commission at no extra cost to you — it helps keep DataEngPrep free.
According to DataEngPrep.tech, this is one of the most frequently asked Python/Coding interview questions, reported at 1 company. DataEngPrep.tech maintains an editor-reviewed database of 1,863 data engineering interview questions across 7 categories.