0
0
LLDsystem_design~20 mins

Dependency injection framework in LLD - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Dependency Injection Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
How does constructor injection behave in this example?

Consider a class Service that depends on Repository. The dependency is injected via the constructor. What will be the output when calling service.getData()?

LLD
class Repository {
  getData() {
    return 'data from repo';
  }
}

class Service {
  constructor(repo) {
    this.repo = repo;
  }
  getData() {
    return this.repo.getData();
  }
}

const repo = new Repository();
const service = new Service(repo);
console.log(service.getData());
ATypeError: this.repo.getData is not a function
B"undefined"
C"data from repo"
D"null"
Attempts:
2 left
💡 Hint

Think about how the Service class uses the injected Repository instance.

🧠 Conceptual
intermediate
2:00remaining
Which option correctly registers a singleton in a dependency injection container?

Given a simple dependency injection container, which option correctly registers a singleton instance of Logger so that all requests get the same instance?

LLD
class Logger {}

class Container {
  constructor() {
    this.services = new Map();
  }
  registerSingleton(name, factory) {
    const instance = factory();
    this.services.set(name, () => instance);
  }
  resolve(name) {
    return this.services.get(name)();
  }
}

const container = new Container();
Acontainer.registerSingleton('logger', new Logger());
Bcontainer.registerSingleton('logger', Logger);
Ccontainer.registerSingleton('logger', () => Logger);
Dcontainer.registerSingleton('logger', () => new Logger());
Attempts:
2 left
💡 Hint

The registerSingleton method expects a factory function that returns an instance.

🔍 Analysis
advanced
2:00remaining
Why does this circular dependency cause a runtime error?

Two classes A and B depend on each other via constructor injection. What error will occur when trying to instantiate A through the container?

LLD
class A {
  constructor(b) {
    this.b = b;
  }
}

class B {
  constructor(a) {
    this.a = a;
  }
}

class Container {
  constructor() {
    this.services = new Map();
  }
  register(name, factory) {
    this.services.set(name, factory);
  }
  resolve(name) {
    const factory = this.services.get(name);
    return factory(this.resolve(name === 'A' ? 'B' : 'A'));
  }
}

const container = new Container();
container.register('A', (b) => new A(b));
container.register('B', (a) => new B(a));
container.resolve('A');
ARangeError: Maximum call stack size exceeded
BReferenceError: b is not defined
CTypeError: factory is not a function
DNo error, returns instance of A
Attempts:
2 left
💡 Hint

Think about what happens when resolve calls itself recursively without a stop condition.

state_output
advanced
2:00remaining
What is the state of the container after registering and resolving a transient service?

Given a container that registers transient services (new instance every resolve), what will be the output of comparing two resolved instances?

LLD
class Service {}

class Container {
  constructor() {
    this.factories = new Map();
  }
  registerTransient(name, factory) {
    this.factories.set(name, factory);
  }
  resolve(name) {
    return this.factories.get(name)();
  }
}

const container = new Container();
container.registerTransient('service', () => new Service());
const s1 = container.resolve('service');
const s2 = container.resolve('service');
console.log(s1 === s2);
Aundefined
Bfalse
Ctrue
DTypeError
Attempts:
2 left
💡 Hint

Transient services create a new instance each time they are resolved.

🧠 Conceptual
expert
2:00remaining
Which option best describes the benefit of using dependency injection frameworks?

Why do developers use dependency injection frameworks in large applications?

AThey allow components to be loosely coupled by managing dependencies externally, improving testability and flexibility.
BThey replace the need for any manual object creation or configuration.
CThey enforce strict type checking at runtime to prevent errors.
DThey automatically optimize application performance by caching all objects globally.
Attempts:
2 left
💡 Hint

Think about how dependencies are managed and how that affects code structure and testing.