0
0
LLDsystem_design~20 mins

Law of Demeter in LLD - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Law of Demeter Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Understanding the Law of Demeter Principle

Which of the following best describes the Law of Demeter in system design?

AA module should have direct access to all parts of the system to maximize flexibility.
BA module should avoid any communication with other modules to remain isolated.
CA module should always call methods on objects returned by other methods to chain operations.
DA module should only communicate with its immediate friends and not with strangers or friends of friends.
Attempts:
2 left
💡 Hint

Think about limiting the knowledge a module has about other parts of the system.

Architecture
intermediate
1:30remaining
Identifying Law of Demeter Violation in Design

In the following design, which interaction violates the Law of Demeter?

Class A calls a method on Class B, which returns an object of Class C. Then Class A calls a method on Class C.

AClass A calling a method directly on Class B.
BClass B returning an object of Class C to Class A.
CClass A calling a method on the object of Class C returned by Class B.
DClass B calling a method on Class C internally.
Attempts:
2 left
💡 Hint

Consider which call crosses multiple object boundaries in one step.

scaling
advanced
2:00remaining
Scaling a System While Respecting Law of Demeter

You are designing a large-scale e-commerce system. To keep the system maintainable, how should you apply the Law of Demeter when components need to access nested data?

ALet components chain multiple method calls on nested objects to get required data.
BProvide higher-level methods in each component to expose only necessary data, avoiding deep object navigation.
CUse global variables to share nested data across components for faster access.
DAllow components to access nested objects directly to reduce code complexity.
Attempts:
2 left
💡 Hint

Think about how to hide internal structure and reduce dependencies.

tradeoff
advanced
1:30remaining
Tradeoffs When Strictly Applying Law of Demeter

What is a common tradeoff when strictly enforcing the Law of Demeter in a complex system?

AIt can lead to excessive wrapper methods, increasing code verbosity and maintenance effort.
BIt always improves performance by reducing method calls.
CIt eliminates the need for interfaces and abstractions.
DIt allows direct access to all nested objects, simplifying debugging.
Attempts:
2 left
💡 Hint

Consider what happens when you hide all internal details strictly.

component
expert
2:00remaining
Refactoring to Comply with Law of Demeter

Given the following code snippet, which refactoring best aligns with the Law of Demeter?

class Order {
  Customer customer;
  public String getCustomerCity() {
    return customer.getAddress().getCity();
  }
}
AAdd a getCity() method in Customer that returns customer.getAddress().getCity(), then Order calls customer.getCity().
BAllow Order to access Address object directly and call getCity() as in the original code.
CMove getCity() method to Order class and have Order call customer.getCity() directly.
DRemove getCustomerCity() method and let external code call customer.getAddress().getCity() directly.
Attempts:
2 left
💡 Hint

Think about which class should hide the internal structure of its data.