Bird
Raised Fist0
LLDsystem_design~20 mins

Law of Demeter in LLD - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1.

What is the main idea behind the Law of Demeter in system design?

easy
A. An object should only communicate with its immediate friends.
B. An object can access any part of the system freely.
C. Objects should share all their internal details with others.
D. Objects must avoid any communication with other objects.

Solution

  1. Step 1: Understand the Law of Demeter principle

    The Law of Demeter states that an object should only talk to its immediate friends, meaning it should not reach deep into other objects.
  2. Step 2: Match the principle to the options

    An object should only communicate with its immediate friends. correctly describes this idea, while others contradict it.
  3. Final Answer:

    An object should only communicate with its immediate friends. -> Option A
  4. Quick Check:

    Law of Demeter = Talk only to immediate friends [OK]
Hint: Remember: Only talk to your immediate friends [OK]
Common Mistakes:
  • Thinking objects can access any part of the system
  • Believing objects should share all internal details
  • Assuming no communication is allowed between objects
2.

Which of the following code snippets follows the Law of Demeter?

class Car {
  Engine engine;
  void start() {
    engine.start();
  }
}
easy
A. Calling engine.start() directly inside Car is allowed.
B. Accessing engine.fuel.tank.level inside Car is allowed.
C. Calling engine.getFuel().getTank().getLevel() is preferred.
D. Car should access engine.fuel.tank.level directly.

Solution

  1. Step 1: Identify allowed communication

    According to the Law of Demeter, Car can call methods on its direct member engine.
  2. Step 2: Analyze the options

    Calling engine.start() directly inside Car is allowed. shows Car calling engine.start(), which is allowed. Options A, B, and C involve accessing deeper nested objects, which breaks the law.
  3. Final Answer:

    Calling engine.start() directly inside Car is allowed. -> Option A
  4. Quick Check:

    Direct member calls = Allowed [OK]
Hint: Only call methods on your direct members [OK]
Common Mistakes:
  • Accessing nested objects directly
  • Chaining multiple getters in one call
  • Ignoring encapsulation principles
3.

Consider this code snippet:

class A {
  B b;
  void doSomething() {
    int x = b.getC().getValue();
    System.out.println(x);
  }
}

Which statement about this code is true regarding the Law of Demeter?

medium
A. It follows the Law of Demeter perfectly.
B. It violates the Law of Demeter by chaining calls.
C. It violates the Law of Demeter by not calling b directly.
D. It follows the Law of Demeter by accessing getValue().

Solution

  1. Step 1: Analyze the method call chain

    The code calls b.getC().getValue(), which chains two calls to reach a nested object.
  2. Step 2: Compare with Law of Demeter

    The Law of Demeter discourages chaining calls like this; an object should only call methods on its direct members.
  3. Final Answer:

    It violates the Law of Demeter by chaining calls. -> Option B
  4. Quick Check:

    Chained calls = Violation [OK]
Hint: Watch out for chained method calls [OK]
Common Mistakes:
  • Thinking chained calls are allowed
  • Confusing direct member calls with nested calls
  • Ignoring encapsulation rules
4.

Identify the problem in this code snippet related to the Law of Demeter and suggest a fix:

class Order {
  Customer customer;
  void printCustomerCity() {
    System.out.println(customer.getAddress().getCity());
  }
}
medium
A. No problem; code follows the Law of Demeter.
B. Violation: Order should print city without any method calls.
C. Violation: Order should access Address directly. Fix by removing Customer.
D. Violation: Order accesses nested Address. Fix by adding getCity() in Customer.

Solution

  1. Step 1: Identify Law of Demeter violation

    Order calls customer.getAddress().getCity(), chaining calls to nested objects.
  2. Step 2: Suggest fix to follow Law of Demeter

    Add a getCity() method in Customer that returns the city, so Order calls only customer.getCity().
  3. Final Answer:

    Violation: Order accesses nested Address. Fix by adding getCity() in Customer. -> Option D
  4. Quick Check:

    Use direct member methods to avoid chaining [OK]
Hint: Add helper methods to avoid deep calls [OK]
Common Mistakes:
  • Ignoring nested call chains
  • Removing needed objects instead of fixing access
  • Trying to print without method calls
5.

You are designing a messaging system where ChatRoom has User objects, and each User has a Profile. To follow the Law of Demeter, how should ChatRoom get a user's display name?

hard
A. Make Profile public and access displayName directly.
B. chatRoom.getUser().getProfile().getDisplayName() chaining calls is fine.
C. Add a method getDisplayName() in User and call chatRoom.getUser().getDisplayName().
D. Let ChatRoom access Profile objects directly.

Solution

  1. Step 1: Identify Law of Demeter violation in chaining

    Chaining calls like getUser().getProfile().getDisplayName() breaks the Law of Demeter.
  2. Step 2: Apply Law of Demeter by adding helper method

    Adding getDisplayName() in User lets ChatRoom call only one method on its direct friend User.
  3. Final Answer:

    Add a method getDisplayName() in User and call chatRoom.getUser().getDisplayName(). -> Option C
  4. Quick Check:

    Use helper methods to avoid deep chains [OK]
Hint: Use helper methods in immediate objects [OK]
Common Mistakes:
  • Allowing chained calls
  • Making internal objects public
  • Accessing nested objects directly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard