Software Engineer Interview Question

What is dependency injection and what problem does it actually solve?

What the interviewer is probing, how to structure your answer, and a spoken example you can adapt.

Quick answer

Dependency injection means a component receives its collaborators from outside instead of constructing them itself. The problem it solves is coupling: if a class creates its own database client, you cannot swap it, test it in isolation, or control its lifetime. Passing dependencies in through the constructor makes them explicit, makes the class honest about what it touches, and lets you supply a fake in tests or a different implementation per environment.

Why interviewers ask this

This checks whether you understand a principle or just use a framework annotation. Interviewers want the coupling and testability argument, the insight that constructor parameters document a class's real dependencies, and the maturity to say you do not need a container to do it. Candidates who cannot explain the benefit without naming a framework usually have not felt the pain it fixes.

How to structure your answer

  • Define it as passing collaborators in rather than constructing them.
  • Name the two benefits: testability and swappability.
  • Point out it needs no framework, only constructor parameters.
  • Give one example where it saved you real time.

Example answer

Spoken example, first person

It just means a class is handed what it needs instead of building it. If my order service constructs its own Postgres client in the constructor, it is welded to Postgres and to whatever connection string is in the environment, and every test of that class is now an integration test. If it takes a repository interface as a constructor parameter, I pass the real one in production and an in memory one in tests, and the class does not care. The benefit I have come to value more over time is honesty: the constructor signature tells you exactly what this thing touches, so when a class takes seven dependencies that is a design smell you can see rather than one you go looking for. I do want to be clear that I do not think you need a container. On smaller services I wire everything by hand in one composition file, and that file becomes the map of the system. Frameworks help once wiring gets tedious, not before.

Walking into this interview soon? GhostPilot listens to your live call, spots the question the moment it is asked, and puts a structured answer on your screen in real time. Try it on your next mock, or grab a $29 Session Pass, no subscription, for the real thing.

See how it works

Follow-up questions to expect

  • When does a DI container add more complexity than it removes?
  • How do you manage the lifetime of an injected dependency?
  • How is this different from the service locator pattern?

Related software engineer questions

Your interviewer will ask their own version of this. Paste your actual job description into the free Question Predictor and get the 20 questions that role is most likely to ask, with what each one is really probing.

Predict my questions

Rehearse the hard questions before they are asked

Practise with a live copilot, then walk in ready. A $29 Session Pass gets you through the interview with no subscription and no lock-in.

Get GhostPilot