← Amazon Interview Insights

Amazon·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Apr 2026

Summary

Amazon technical phone screen for a software engineer role, one question that went deeper than I expected once they started pushing on Python-specific behavior.

Questions Asked (1)

Q1

What's the difference between a constructor and a destructor in OOP? And does Python actually have a destructor?

Technical Trade-offsSystem Design
Author's notes

Started fine, constructor vs destructor is pretty basic, but then they kept pulling on the Python thread and I got a bit shaky.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by defining constructors and destructors in general OOP terms, then contrast their purposes and lifecycles. Next, address Python's specific behavior, clarifying that while it has an initializer (__init__) and a finalizer (__del__), the latter is not a true destructor due to garbage collection. Finally, discuss practical implications and trade-offs, especially in managed memory environments like Python.

Pro tip: Emphasize that in Python, __del__ is unreliable for cleanup due to garbage collection nuances, and recommend using context managers (with statement) for deterministic resource management. This shows depth and practical experience.

1. Define Constructor and Destructor

Explain that a constructor initializes a new object, setting up its state, while a destructor cleans up resources when an object is destroyed. Mention that constructors are called automatically upon object creation, and destructors upon object destruction.

2. Contrast Their Roles and Lifecycles

Highlight that constructors are essential for object initialization and are called once per object, whereas destructors handle cleanup and are called once when the object is no longer needed. Discuss how destructors are less predictable in garbage-collected languages.

3. Address Python's Specifics

Clarify that Python has an initializer method __init__ (often called a constructor) and a finalizer __del__ (sometimes called a destructor). Explain that __del__ is not guaranteed to be called promptly or at all, as it depends on the garbage collector.

4. Discuss Practical Implications and Alternatives

Mention that relying on __del__ for cleanup is discouraged; instead, use context managers (with statement) or explicit close methods for deterministic resource release. This is crucial in production code.

5. Summarize with Trade-offs

Conclude that while the concept of destructors exists in Python, it's not a true destructor in the C++ sense. Emphasize the importance of understanding memory management models when designing systems.

Key Points to Mention

  • Constructor (__init__) initializes object state; called automatically after instance creation.
  • Destructor (__del__) is called when object is about to be destroyed, but timing is non-deterministic in Python.
  • Python uses garbage collection, so __del__ may not be called immediately or at all (e.g., circular references).
  • __del__ can be problematic: exceptions in __del__ are ignored, and it can resurrect objects.
  • Recommended alternatives: context managers (with statement), try/finally blocks, or explicit cleanup methods.
  • In languages like C++, destructors are deterministic and crucial for RAII; Python's model differs significantly.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.