Questions
7 of 19
1How would you make a custom class usable in a for loop?
2How do __enter__ and __exit__ work together to implement the context manager protocol?
3How does Python implement inheritance, and what is the Method Resolution Order (MRO)?
4What are dataclasses, and how do they reduce boilerplate compared to manually writing __init__, __eq__, and __repr__?
5What is the @property decorator, and what problem does it solve compared to plain getter/setter methods?
6What is the difference between method overriding and method overloading, and why doesn't Python natively support overloading?
7What problem does the diamond inheritance pattern cause, and how does Python's MRO resolve it?
8What is the difference between a class and an instance in Python?
9How does Python implement encapsulation given that it has no true 'private' access modifiers?
10What is the purpose of self in instance methods, and why must it be explicitly declared in Python?
11What is the difference between __str__ and __repr__, and what convention should __repr__ follow?
12What is a metaclass, and what is type's relationship to every class in Python?
13What is the purpose of __eq__ and __hash__, and why must they be implemented consistently?
14What is a descriptor, and how do @property and ORMs like Django's models rely on the descriptor protocol?
15What is the difference between an instance method, a class method, and a static method?
16What is composition, and why is 'favor composition over inheritance' often recommended?
17What are dunder (magic) methods, and how do they enable operator overloading?
18When would you use a metaclass instead of a class decorator or __init_subclass__?
19How do abstract base classes (abc.ABC) enforce interface contracts in Python?
07 / 19

What problem does the diamond inheritance pattern cause, and how does Python's MRO resolve it?

Difficulty: 8/10
Multiple Inheritance, Diamond Problem, MRO, Super

Diamond inheritance duplicates ancestors; C3 MRO linearizes so each class appears once

The diamond problem appears when two classes inherit from a common base and a fourth class inherits from both. In languages without a clear rule, the base can be initialized or overridden twice, causing duplicated state or ambiguous method dispatch. Python solves this statically: C3 linearization produces a single order that includes each ancestor exactly once, and preserves the relative order of bases. So in class D(B, C) where B and C both derive from A, the MRO is D, B, C, A, object. With cooperative super() calls, init and other methods run exactly once per class in the chain.

  1. 1

    The diamond is not inherently a bug; it becomes a bug when base classes are not written cooperatively.

  2. 2

    Cooperative pattern: each init calls super().init(**kwargs) and pops only the arguments it consumes.

  3. 3

    Trade-off: cooperative super() keeps the chain consistent but makes signatures harder to read and breaks naive direct calls.

  4. 4

    Common mistake: calling BaseClass.init(self) explicitly, which bypasses the MRO and can run a class twice or skip one entirely.

  5. 5

    Common mistake: assuming the leftmost base wins for every attribute. The MRO decides, and it may put a right-side class before a shared ancestor.

  6. 6

    Version note: the diamond behavior described is C3 linearization from Python 2.3 onward and is uniform across Python 3.

javascript

Scenario Questions

0-2 years experience

  1. 1Two parent classes both define greet. Which one runs for the child class?
  2. 2What is the MRO of D when class D(B, C) and both B and C inherit from A?

2-5 years experience

  1. 1You call Parent.__init__(self) directly in a diamond hierarchy and one class runs twice. How do you fix it with super()?
  2. 2A mixin defines an attribute also defined by a sibling mixin. How do you reason about which wins?

5-8 years experience

  1. 1You add a logging mixin into a deep hierarchy and logs appear twice. How do you diagnose and fix the super() chain?
  2. 2You are designing a plugin hierarchy where multiple base plugins define hooks. How do you ensure each hook runs exactly once?

8+ years experience

  1. 1Design a cooperative mixin framework where every class accepts **kwargs and validates required parameters, without breaking on unexpected keys.
  2. 2Explain how C3 linearization interacts with metaclass conflicts and how to resolve a metaclass MRO clash in a real framework.

Follow-up Questions

  • What goes wrong if one class in the diamond does not call super().__init__?
  • How would you make cooperative multiple inheritance work with required positional arguments?
Share

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