Inheritance, polymorphism, constructors and destructors
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Amazon's technical phone screen, last asked Jan 2026. It tests object-oriented programming and python.
Answer a set of object-oriented programming questions, and back up your answers with a little code.
Part 1: inheritance and polymorphism
Explain how inheritance and polymorphism differ and how they relate. Then write a short example: a parent class with a method, a child class that overrides it, and a function that calls the method through a reference to the parent type, so the child's version runs.
Part 2: constructors and destructors
Compare constructors and destructors: when each runs, what each is for, and in what order they run when a child class extends a parent.
Part 3: destructors in Python
Does Python have destructors? Explain __del__, when it runs, and why you usually should not rely on it.
Clarifying questions
- Which language should the code be in?
- Should the example show overloading as well as overriding?
What a strong answer covers
- Inheritance as a relationship between classes (reuse, "is a"); polymorphism as one call site doing different things depending on the object's runtime type; overriding as the mechanism that connects them.
- Overriding versus overloading, and how dynamic dispatch works (virtual methods in C++, every method in Python and Java).
- Construction runs parent first, then child; destruction runs in reverse. In C++ a base class needs a virtual destructor if objects are deleted through a base pointer.
- In Python,
__del__runs when the last reference goes away, which is not guaranteed to happen at a predictable time (reference cycles, other interpreters, interpreter shutdown). Release resources withwithblocks and context managers instead.
Follow-ups
- When would you prefer composition to inheritance?
- What is the diamond problem, and how do C++ and Python handle it?
- Show an abstract base class that forces subclasses to implement a method.
Where it's asked
- Amazon · Technical phone screen · Last asked Jan 202660 min · Live coding in a shared editor that does not run code, plus Leadership Principles questions · No AI
What it tests
object-oriented programming · python
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
