0
0
LLDsystem_design~20 mins

Composition over inheritance in LLD - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Composition Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why prefer composition over inheritance?

Which of the following best explains why composition is often preferred over inheritance in system design?

AInheritance avoids code duplication better than composition in every scenario.
BComposition allows changing behavior at runtime by swapping components, while inheritance fixes behavior at compile time.
CComposition requires fewer classes than inheritance in all cases.
DInheritance always leads to faster code execution than composition.
Attempts:
2 left
💡 Hint

Think about flexibility and how behavior can be changed after the system is running.

Architecture
intermediate
2:00remaining
Design choice for a payment system

You are designing a payment system that supports multiple payment methods (credit card, PayPal, crypto). Which design approach best uses composition over inheritance?

AUse inheritance to create subclasses for each payment type and hardcode the payment logic in each subclass.
BCreate a PaymentMethod interface and have each payment type inherit from it, implementing their own logic.
CCreate separate classes for each payment type with no shared interface or composition.
DCreate a PaymentProcessor class that contains a PaymentMethod component which can be swapped to change payment type at runtime.
Attempts:
2 left
💡 Hint

Consider how you can change payment methods without changing the main processor class.

scaling
advanced
2:30remaining
Scaling a notification system with composition

You have a notification system that sends alerts via email, SMS, and push notifications. To scale and add new channels easily, which design is best?

AUse composition where a NotificationSender class contains a list of NotificationChannel components that can be added or removed dynamically.
BHardcode all notification types inside one large Notification class using if-else statements.
CUse inheritance to create subclasses for each notification type and add new subclasses for new channels.
DCreate separate monolithic services for each notification type without shared components.
Attempts:
2 left
💡 Hint

Think about how to add or remove notification channels without changing existing code.

tradeoff
advanced
2:30remaining
Tradeoffs between composition and inheritance

Which of the following is a valid tradeoff when choosing composition over inheritance?

AInheritance always results in simpler code and fewer classes than composition.
BComposition does not allow code reuse, unlike inheritance which always does.
CComposition can increase the number of objects and indirection, potentially impacting performance compared to inheritance.
DInheritance allows changing behavior at runtime, while composition fixes behavior at compile time.
Attempts:
2 left
💡 Hint

Consider the overhead of managing multiple objects versus a class hierarchy.

component
expert
3:00remaining
Component interaction in a compositional design

In a compositional design for a media player, the Player class composes components: Decoder, Renderer, and Logger. Which sequence best describes the request flow when playing a media file?

A1, 2, 3, 4
B1, 3, 2, 4
C2, 1, 3, 4
D1, 4, 2, 3
Attempts:
2 left
💡 Hint

Think about the logical order of processing from request to output and logging.