Questions
16 of 24
1How does NestJS resolve constructor injection at runtime?
2What does @Optional() decorator do in NestJS?
3What is the difference between moduleRef.get() and moduleRef.resolve() in NestJS?
4When do you need @Inject(TOKEN) vs plain constructor injection in NestJS?
5What is ModuleRef in NestJS and when would you use it?
6What are the three provider scopes in NestJS and when is each appropriate?
7What is the correct architectural solution for circular dependencies beyond forwardRef()?
8How do you detect and debug circular dependency errors in large NestJS projects?
9What is property-based injection in NestJS and when should you use it?
10How do you write a unit test for a NestJS service that has injected dependencies?
11What is a DI token and what types can be used as tokens in NestJS?
12How do you pass a custom REQUEST object to a REQUEST-scoped provider for queues or CRON jobs in NestJS?
13How does forwardRef() solve circular dependencies in NestJS and how does it work internally?
14How do you implement the Strategy pattern using NestJS DI?
15How do you replace a provider in a NestJS test module using overrideProvider()?
16What is Inversion of Control (IoC) and how does NestJS implement it?
17What is the difference between Dependency Injection (DI) and Inversion of Control (IoC)?
18What is scope propagation in NestJS and why can it impact performance?
19How do you inject the raw HTTP request object inside a REQUEST-scoped provider in NestJS?
20How does moduleRef.resolve() work with scoped providers and what is ContextIdFactory used for?
21What role does reflect-metadata play in NestJS DI?
22What is a circular dependency in NestJS and how does it manifest at runtime?
23What is LazyModuleLoader in NestJS and how does it differ from standard DI?
24What happens to REQUEST-scoped providers during WebSocket connections or microservice message handling in NestJS?
16 / 24

What is Inversion of Control (IoC) and how does NestJS implement it?

IoC is a design principle where an object's dependencies are provided externally rather than created internally. NestJS implements IoC through its built-in container: you declare what a class needs via constructor parameters, and the container instantiates and injects those dependencies automatically.

Inversion of Control means the framework controls the flow of the program instead of your application code. Rather than a class instantiating its own dependencies, those dependencies are created and provided by an external container.

Without IoC vs with IoC
Benefits of IoC in NestJS:
  1. 1

    Classes are decoupled from their dependencies — easier to swap implementations.

  2. 2

    Dependencies can be mocked in unit tests without modifying production code.

  3. 3

    The container manages object lifecycle, including singleton enforcement.

  4. 4

    Reduces boilerplate — no manual new() calls scattered across the codebase.

Difficulty: 6/10
Topics: Dependency Injection, Module System, Provider Lifecycle

Scenario Questions

0-2 years experience
  1. 1

    You're trying to inject a Logger service into your UserService, but it's coming back as undefined — what are the two most likely reasons and how would you fix them?

  2. 2

    You created a new service and added it to a module, but when you try to use it in a controller, Nest throws a 'Cannot resolve dependency' error. What step did you probably forget?

2-5 years experience
  1. 1

    A feature that worked fine in dev started failing in staging with a circular dependency error between AuthService and PermissionService — how would you trace and resolve this without breaking the business logic?

  2. 2

    Your team added a new provider with request scope to handle user context, but now requests are slower and memory usage is climbing — what’s likely happening and how would you investigate?

5-8 years experience
  1. 1

    You're designing a multi-tenant system where each tenant needs a separate database connection — how would you structure providers and modules to ensure isolation, scalability, and clean testability without leaking state?

  2. 2

    A legacy module is using a global provider that’s now causing race conditions under load. How would you refactor it to use scoped providers while maintaining backward compatibility and minimizing downtime?

8+ years experience
  1. 1

    You're leading the migration of a monolithic NestJS app to microservices — how would you redesign the IoC structure to avoid cross-service provider pollution while preserving reusability and enabling independent deployments?

  2. 2

    An external team is consuming your core module as an npm package, but they're accidentally overriding your internal providers. What architectural safeguards would you implement to enforce encapsulation and prevent runtime breakage in consumer apps?

Follow-up Questions

  • How would you debug a service that's not being injected properly?
  • What happens if two modules try to provide the same token with different implementations?
  • Why would you choose request scope over singleton for a database connection?