Bird
Raised Fist0
Pythonprogramming~20 mins

Diamond problem in Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Diamond Inheritance Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of method call in diamond inheritance
Consider the following Python code using diamond inheritance. What is the output when obj.greet() is called?
Python
class A:
    def greet(self):
        return "Hello from A"

class B(A):
    def greet(self):
        return "Hello from B"

class C(A):
    def greet(self):
        return "Hello from C"

class D(B, C):
    pass

obj = D()
print(obj.greet())
A"Hello from C"
B"Hello from A"
C"Hello from B"
DTypeError
Attempts:
2 left
💡 Hint
Remember Python uses Method Resolution Order (MRO) to decide which method to call.
Predict Output
intermediate
2:00remaining
Understanding MRO list in diamond inheritance
What is the output of print(D.__mro__) for the following classes?
Python
class A:
    pass

class B(A):
    pass

class C(A):
    pass

class D(B, C):
    pass

print(D.__mro__)
A(<class '__main__.D'>, <class '__main__.A'>, <class '__main__.B'>, <class '__main__.C'>, <class 'object'>)
B(<class '__main__.D'>, <class '__main__.C'>, <class '__main__.B'>, <class '__main__.A'>, <class 'object'>)
C(<class '__main__.D'>, <class '__main__.B'>, <class '__main__.A'>, <class '__main__.C'>, <class 'object'>)
D(<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.A'>, <class 'object'>)
Attempts:
2 left
💡 Hint
MRO follows the order of inheritance from left to right and then up the hierarchy.
Predict Output
advanced
2:00remaining
Output with super() in diamond inheritance
What will be printed when D().greet() is called in this code?
Python
class A:
    def greet(self):
        return "Hello from A"

class B(A):
    def greet(self):
        return "B says " + super().greet()

class C(A):
    def greet(self):
        return "C says " + super().greet()

class D(B, C):
    def greet(self):
        return "D says " + super().greet()

print(D().greet())
A"D says B says C says Hello from A"
B"D says C says B says Hello from A"
C"D says Hello from A"
DAttributeError
Attempts:
2 left
💡 Hint
super() follows the MRO chain calling next method in line.
Predict Output
advanced
2:00remaining
Effect of changing inheritance order on output
What is the output of D().greet() if class D inherits as class D(C, B): instead of class D(B, C): in the previous example?
Python
class A:
    def greet(self):
        return "Hello from A"

class B(A):
    def greet(self):
        return "B says " + super().greet()

class C(A):
    def greet(self):
        return "C says " + super().greet()

class D(C, B):
    def greet(self):
        return "D says " + super().greet()

print(D().greet())
A"D says C says B says Hello from A"
B"D says B says C says Hello from A"
C"D says Hello from A"
DTypeError
Attempts:
2 left
💡 Hint
Changing the order of inheritance changes the MRO and thus the order super() calls methods.
🧠 Conceptual
expert
2:00remaining
Why Python's diamond problem solution is safe
Why does Python's method resolution order (MRO) prevent the classic diamond problem issues seen in some other languages?
ABecause Python uses depth-first search to find methods, avoiding duplicates.
BBecause Python uses a linearization algorithm (C3) that ensures each class appears only once in the MRO, preventing duplicate calls.
CBecause Python requires explicit calls to parent methods, so no automatic method calls happen.
DBecause Python does not support multiple inheritance, so diamond problem cannot occur.
Attempts:
2 left
💡 Hint
Think about how Python orders classes in the MRO list.

Practice

(1/5)
1.

What is the diamond problem in Python's multiple inheritance?

easy
A. A problem where Python cannot find any method in the class hierarchy.
B. A syntax error caused by using multiple inheritance.
C. A situation where a class inherits from two classes that both inherit from the same base class.
D. A situation where a class inherits from only one base class.

Solution

  1. Step 1: Understand multiple inheritance structure

    The diamond problem occurs when a class inherits from two classes that share a common ancestor, forming a diamond shape in the inheritance graph.
  2. Step 2: Recognize the diamond shape

    This shape causes ambiguity in method resolution because the common base class appears twice in the inheritance path.
  3. Final Answer:

    A situation where a class inherits from two classes that both inherit from the same base class. -> Option C
  4. Quick Check:

    Diamond problem = multiple inheritance with shared base [OK]
Hint: Diamond problem = shared base class inherited twice [OK]
Common Mistakes:
  • Thinking diamond problem is a syntax error
  • Confusing single inheritance with diamond problem
  • Believing diamond problem means no methods found
2.

Which of the following class definitions correctly shows multiple inheritance that can cause the diamond problem?

class A:
    pass

class B(A):
    pass

class C(A):
    pass

class D(??):
    pass

What should replace ???

easy
A. A, B
B. B, C
C. C, B
D. A, C

Solution

  1. Step 1: Identify classes inheriting from A

    Classes B and C both inherit from A, so they form the upper branches of the diamond.
  2. Step 2: Define class D inheriting from B and C

    To create the diamond shape, D must inherit from both B and C.
  3. Final Answer:

    B, C -> Option B
  4. Quick Check:

    Diamond bottom inherits from both branches [OK]
Hint: Diamond bottom class inherits from both intermediate classes [OK]
Common Mistakes:
  • Using A directly in D's inheritance
  • Swapping order without reason
  • Using only one parent class
3.

What will be the output of this code?

class A:
    def greet(self):
        print('Hello from A')

class B(A):
    def greet(self):
        print('Hello from B')

class C(A):
    def greet(self):
        print('Hello from C')

class D(B, C):
    pass

d = D()
d.greet()
medium
A. Hello from B
B. Hello from A
C. Hello from C
D. Hello from D

Solution

  1. Step 1: Understand method resolution order (MRO)

    Class D inherits from B and C. Python looks for greet() in D, then B, then C, then A.
  2. Step 2: Identify which greet() is called

    D has no greet(), so it calls B's greet() first, printing 'Hello from B'.
  3. Final Answer:

    Hello from B -> Option A
  4. Quick Check:

    MRO chooses first parent method [OK]
Hint: MRO calls first parent's method in order [OK]
Common Mistakes:
  • Assuming C's greet() is called
  • Expecting A's greet() to run
  • Thinking D has greet() method
4.

Find the error in this code related to the diamond problem:

class A:
    def greet(self):
        print('Hello from A')

class B(A):
    def greet(self):
        print('Hello from B')

class C(A):
    def greet(self):
        print('Hello from C')

class D(B, C):
    def greet(self):
        C.greet(self)

D().greet()
medium
A. Calling C.greet(self) ignores B's greet and breaks MRO.
B. Missing super() call causes infinite recursion.
C. Syntax error in class D definition.
D. No error; code runs fine.

Solution

  1. Step 1: Analyze method call in D.greet()

    D.greet() calls C.greet(self) directly, skipping B's greet() and ignoring MRO.
  2. Step 2: Understand diamond problem and MRO importance

    By calling C.greet(self) directly, it breaks the expected MRO chain and can cause unexpected behavior.
  3. Final Answer:

    Calling C.greet(self) ignores B's greet and breaks MRO. -> Option A
  4. Quick Check:

    Direct base class call breaks MRO [OK]
Hint: Avoid direct base class calls; use super() to respect MRO [OK]
Common Mistakes:
  • Thinking code has syntax error
  • Missing that direct call breaks MRO
  • Assuming no error occurs
5.

Given this class structure, what will be the output of D().greet()?

class A:
    def greet(self):
        print('Hello from A')

class B(A):
    def greet(self):
        print('Hello from B')
        super().greet()

class C(A):
    def greet(self):
        print('Hello from C')
        super().greet()

class D(B, C):
    def greet(self):
        print('Hello from D')
        super().greet()

D().greet()
hard
A. Hello from D Hello from C Hello from A
B. Hello from D Hello from C Hello from B Hello from A
C. Hello from D Hello from B Hello from A
D. Hello from D Hello from B Hello from C Hello from A

Solution

  1. Step 1: Determine MRO for class D

    D's MRO is D, B, C, A, object. super() calls follow this order.
  2. Step 2: Trace greet() calls

    D.greet() prints 'Hello from D' then calls B.greet(), which prints 'Hello from B' and calls C.greet(), which prints 'Hello from C' and calls A.greet(), which prints 'Hello from A'.
  3. Final Answer:

    Hello from D Hello from B Hello from C Hello from A -> Option D
  4. Quick Check:

    super() follows MRO chain [OK]
Hint: super() calls follow MRO order in diamond inheritance [OK]
Common Mistakes:
  • Ignoring MRO order in super() calls
  • Assuming C's greet() runs before B's
  • Missing that all greet() methods run