Practice
1. In designing the
User and Message classes for the messaging system, should User be an abstract class or an interface, and why?easy
Solution
Step 1: Analyze if User has state or just behavior
User holds common state such as userId and username, which requires fields and possibly default implementations.Step 2: Determine if abstract class or interface fits state and behavior
Abstract class supports shared state and partial implementation, while interface cannot hold instance state.Final Answer:
Option C -> Option CQuick Check:
User has common state -> abstract class fits better than interface [check]
Hint: Stateful base class -> abstract class, stateless -> interface [check]
Common Mistakes:
2. In designing the ESPNcricinfo system, should the
Ball entity be modeled as an abstract class or an interface? Consider that Ball has state like ballNumber and behavior like calculateRuns(). Which is the best choice and why?easy
Solution
Step 1: Analyze Ball's state and behavior
Ball has state (ballNumber) and behavior (calculateRuns), so it needs to store data and provide common logic.Step 2: Abstract class vs interface for state
Interfaces cannot hold instance state; abstract classes can hold state and provide shared implementation.Final Answer:
Option A -> Option AQuick Check:
Stateful entity with behavior -> abstract class [check]
Hint: Stateful with behavior -> abstract class, stateless behavior -> interface [check]
Common Mistakes:
3. To add a
WeekendFeeStrategy for locker usage fees in the current Amazon Locker design, which minimal change respects SOLID principles?medium
Solution
Step 1: Identify the SOLID principle involved
Open-Closed Principle (OCP) requires adding new behavior without modifying existing code.Step 2: Evaluate options for minimal change respecting OCP
Create a new WeekendFeeStrategy class implementing FeeStrategy and inject it without changing existing classes. suggests adding a new strategy class, but injecting it requires modifying existing FeeCalculator or client code, violating minimal change. Add a boolean isWeekend flag to Package and modify LockerManager to apply extra fees conditionally. adds a flag and conditional logic, which is a minimal change localized to Package and LockerManager, preserving OCP better than modifying FeeCalculator or Locker.Final Answer:
Option D -> Option DQuick Check:
Minimal change with conditional flag -> respects OCP [check]
Hint: OCP: add new behavior without modifying existing classes [check]
Common Mistakes:
4. The brokerage system currently calculates fees using a
FeeCalculator class. To add a new WeekendFeeStrategy without modifying existing code, which SOLID principle applies and what is the minimum change required?medium
Solution
Step 1: Identify the SOLID principle for extensibility
Open/Closed Principle (OCP) states software entities should be open for extension but closed for modification.Step 2: How to apply OCP here
Create a new class WeekendFeeStrategy implementing FeeStrategy interface; FeeCalculator uses polymorphism to apply different strategies without modification.Final Answer:
Option A -> Option AQuick Check:
New strategy class + polymorphism -> OCP compliance [check]
Hint: Add new fee strategy class, no FeeCalculator changes [check]
Common Mistakes:
5. You need to add support for
ElectricCar that requires ChargingSpot compatibility in the current parking system. What is the minimum change to the design to support this without modifying existing classes?hard
Solution
Step 1: Identify extensibility requirements
ElectricCar and ChargingSpot are new types requiring compatibility mapping without changing existing classes.Step 2: Apply minimal invasive change
Adding new enum values and subclass plus updating compatibility map extends functionality without modifying existing class code, preserving OCP.Step 3: Avoid violating OCP
Options B, C, and D modify existing classes or create parallel hierarchies, increasing complexity and violating OCP.Final Answer:
Option B -> Option BQuick Check:
Extend enums and map, subclass ElectricCar -> minimal change [check]
Hint: Extend enums and map, subclass new type -> minimal change [check]
Common Mistakes:
