L2 · Working engineerObject-oriented Python~5 min · 3 tests#80

Composition vs Inheritance: Car Has an Engine

Model 'a Car has an Engine' with composition instead of inheritance, delegating start() to the engine. Includes dependency injection. Tested live.

The problem

  • Engine has running = False and start() that sets running = True and returns "Engine started".
  • Car(engine) has an engine (composition — Car does not inherit from Engine). Car.start() starts its engine, then returns "Car started".

Examples

  1. Example 1

    Input

    probe(Car, Engine)

    Expected output

    ('Car started', True, False)
  2. Example 2

    Input

    Engine().start()

    Expected output

    'Engine started'

+ 1 hidden test on Submit — any engine can be injected.

Edge cases to ask about

  • Injected fake engine
  • Car is not an Engine
How the tests call your code

These helpers run before your code. The test inputs above call them.

def probe(Car, Engine):
    e = Engine()
    c = Car(e)
    msg = c.start()
    return (msg, e.running, isinstance(c, Engine))

class FakeEngine:
    def __init__(self):
        self.calls = 0
    def start(self):
        self.calls += 1
        return "fake"

def with_fake(Car):
    f = FakeEngine()
    Car(f).start()
    return f.calls

Hints

0/3

    How an interviewer scores this

    0/9
    Python 3.13 · Car
    ⌘/Ctrl + Enter runs the examples

    Your 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.

    Time complexity of the optimal solution
    Space complexity (extra memory)

    Pick both to reveal the answer.

    From brute force to optimal

    The progression an interviewer wants to hear, one step at a time.

    ApproachTimeSpaceIdea
    Composition + delegationO(1)O(1)Pass the engine in (dependency injection) so tests can swap it.
    Walkthrough of the optimal approach (try it yourself first)

    Inheritance says is-a; composition says has-a. A Car has an Engine, so it holds one and delegates to it. Passing the engine into the constructor (dependency injection) means a test can hand in a fake engine — the hidden test does exactly that.

    "Favour composition over inheritance" is the design principle interviewers want named.

    Complexity: O(1) time, O(1) space. One delegated call.

    Reveal the reference solution
    class Engine:
        def __init__(self):
            self.running = False
    
        def start(self):
            self.running = True
            return "Engine started"
    
    
    class Car:
        def __init__(self, engine):
            self.engine = engine
    
        def start(self):
            self.engine.start()
            return "Car started"

    Follow-ups interviewers ask

    • Add an ElectricEngine without changing Car.
    • What breaks if Car inherited from Engine?

    Frequently asked interview questions

    Core interview concepts, complexities, and follow-ups scored by hiring teams.

    What is the time complexity of Composition vs Inheritance: Car Has an Engine in Python?

    The optimal solution runs in O(1) time and O(1) auxiliary space. One delegated call.

    What follow-up questions do interviewers ask about Composition vs Inheritance: Car Has an Engine?

    Add an ElectricEngine without changing Car. What breaks if Car inherited from Engine?