Override a parent method in a Python subclass and still call the parent version with super(). Polymorphism explained with an Animal/Dog example.
The problem
Animal.speak()returns"Some sound".Dog(Animal)overridesspeak()to return"Bark".Dog.both()returns a tuple(parent's speak, own speak)usingsuper()—("Some sound", "Bark").
Examples
Example 1
Input
(Animal().speak(), Dog().speak())
Expected output
('Some sound', 'Bark')Example 2
Input
Dog().both()
Expected output
('Some sound', 'Bark')
+ 1 hidden test on Submit — polymorphism.
Edge cases to ask about
- Calling the parent version
- Mixed collections
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 |
|---|---|---|---|
| Override + super() | O(1) | O(1) | super() delegates to the next class in the MRO. |
Walkthrough of the optimal approach (try it yourself first)
Defining speak in Dog overrides Animal's version: self.speak() on a Dog finds Dog's method first. super().speak() skips to the next class in the MRO, so you can extend rather than replace parent behaviour.
Calling the same method on a mixed list of Animals and Dogs and getting the right behaviour for each is polymorphism.
Complexity: O(1) time, O(1) space. Constant-time method dispatch.
Reveal the reference solution
class Animal: def speak(self): return "Some sound" class Dog(Animal): def speak(self): return "Bark" def both(self): return super().speak(), self.speak()
Follow-ups interviewers ask
- Call super().init correctly in a subclass constructor.
- What does super() do with multiple inheritance?
Frequently asked interview questions
Core interview concepts, complexities, and follow-ups scored by hiring teams.
What is the time complexity of Method Overriding and super() in Python?
The optimal solution runs in O(1) time and O(1) auxiliary space. Constant-time method dispatch.
What follow-up questions do interviewers ask about Method Overriding and super()?
Call super().__init__ correctly in a subclass constructor. What does super() do with multiple inheritance?
