← Walmart Labs Interview Insights

Walmart Labs·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
May 2026

Summary

Interviewed for a SWE role at Walmart Labs, got an OOP design question that felt pretty standard but tripped me up more than I expected.

Questions Asked (1)

Q1

What is the difference between inheritance and composition, and when would you choose one over the other?

Technical Trade-offsSystem Design
Author's notes

I knew the textbook answer but fumbled when they pushed on the 'when to use which' part.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Define inheritance and composition clearly, emphasizing that inheritance models an 'is-a' relationship while composition models a 'has-a' relationship. Then discuss trade-offs such as flexibility, coupling, and code reuse, and give a concrete example where you chose one over the other based on requirements.

Pro tip: Mention that composition is often preferred in modern software design because it avoids the fragile base class problem and supports easier testing and evolution, but acknowledge that inheritance still has valid uses when there is a true subtype relationship and behavior is stable.

1. Define both concepts

Clearly state that inheritance is a mechanism where a class derives from a parent class, inheriting its properties and methods, representing an 'is-a' relationship. Composition is a design principle where a class contains instances of other classes, representing a 'has-a' relationship.

2. Compare trade-offs

Discuss how inheritance creates tight coupling between parent and child, can lead to the fragile base class problem, and limits flexibility because a class can only inherit from one parent (in single inheritance languages). Composition offers greater flexibility, promotes loose coupling, and allows behavior to be composed dynamically at runtime.

3. Provide examples

Give a concrete example, such as a Car class inheriting from Vehicle (inheritance) versus a Car class having an Engine and Transmission (composition). Explain how the composition example allows easier swapping of components and testing.

4. Explain when to choose each

State that inheritance is appropriate when there is a clear, stable 'is-a' relationship and you want to reuse behavior from a base class. Composition is preferable when you need flexibility, want to avoid deep hierarchies, or when the relationship is 'has-a' or 'uses-a'.

5. Relate to real-world scenarios

Tie your answer to a project or system design context, such as designing a payment system at Walmart Labs, where you might use composition to combine different payment methods and avoid a rigid inheritance hierarchy.

Key Points to Mention

  • Inheritance represents an 'is-a' relationship, while composition represents a 'has-a' relationship.
  • Inheritance can lead to tight coupling and the fragile base class problem, making code harder to change.
  • Composition provides greater flexibility, as behaviors can be changed at runtime by swapping components.
  • Favor composition over inheritance when you need to reuse code without forcing a type hierarchy.
  • Use inheritance when there is a genuine subtype relationship and the base class is designed for extension.
  • Design patterns like Strategy and Decorator rely on composition to achieve flexible behavior.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.