Practice
1. The ATM system currently calculates transaction fees using a
FeeCalculator class. To add a WeekendFeeStrategy without modifying existing classes, which SOLID principle applies and what is the minimal correct change?medium
Solution
Step 1: Identify applicable SOLID principles
Open-Closed Principle (OCP) requires FeeCalculator not be modified for new fee types. Dependency Inversion Principle (DIP) requires FeeCalculator depend on abstractions.Step 2: Apply correct design
Create WeekendFeeStrategy implementing FeeStrategy interface. Inject FeeStrategy into FeeCalculator. This adds new behavior without modifying existing code.Final Answer:
Option C -> Option CQuick Check:
New strategy class + DI -> OCP and DIP respected [check]
Hint: New fee? Add strategy class, inject via interface [check]
Common Mistakes:
2. In the in-memory file system, the
createFile() method checks if a filename is available and then adds the file to the directory without synchronization. Which exact thread interleaving causes two threads to create files with the same name (double creation)?medium
Solution
Step 1: Trace the check-then-act sequence without synchronization
Thread 1 calls isNameAvailable() -> true, but before adding the file, context switches to Thread 2.Step 2: Thread 2 also calls isNameAvailable() -> true and proceeds to add the file, then Thread 1 resumes and adds its file too.
This causes double creation due to the race condition on the availability check and add operation.Final Answer:
Option A -> Option AQuick Check:
Check-then-act gap without sync -> double creation [check]
Hint: Check availability -> context switch -> both add same file [check]
Common Mistakes:
3. You want to add a
WeekendFeeStrategy to the parking lot pricing system. Which minimum change respects SOLID principles?medium
Solution
Step 1: Identify SOLID principle involved
Open-Closed Principle (OCP) requires adding new functionality without modifying existing code.Step 2: Evaluate options
Modify the existingFeeCalculatorclass to add weekend logic inside. violates OCP by modifying existing class. Create a newWeekendFeeStrategyclass implementingFeeStrategyand inject it without changing existing classes. respects OCP by adding new strategy class. Add a boolean flagisWeekendtoVehicleand adjust pricing accordingly. incorrectly mixes state into Vehicle, violating SRP. Create a subclassWeekendFeeCalculatorextendingFeeCalculatorand override methods. adds subclass but may cause class explosion and tight coupling.Final Answer:
Option A -> Option AQuick Check:
New strategy class + no existing class change -> OCP respected [check]
Hint: Add new strategy class, avoid modifying existing code [check]
Common Mistakes:
4. You want to add a
WeekendFeeStrategy to calculate weekend parking fees without modifying existing classes. Which SOLID principle guides the minimum change, and what is the correct approach?medium
Solution
Step 1: Identify the SOLID principle for extensibility
Open/Closed Principle (OCP) requires adding new functionality without modifying existing code.Step 2: Evaluate approaches
Creating a new FeeStrategy implementation and injecting it respects OCP and DIP; modifying ParkingLot or existing strategies violates OCP or mixes responsibilities.Final Answer:
Option B -> Option BQuick Check:
New FeeStrategy class + injection -> OCP and DIP respected [check]
Hint: New strategy class + injection -> Open/Closed Principle [check]
Common Mistakes:
5. You need to add support for
ElectricCar and ChargingSpot in the parking system without modifying existing classes. What is the minimum change to the current design?hard
Solution
Step 1: Identify extensibility without modifying existing classes
Enums can be extended with new constants, and new subclasses can be added without changing existing code.Step 2: Use external compatibility map update
Updating the compatibility map externally avoids modifying existing classes, preserving OCP and avoiding class explosion.Step 3: Avoid flags or interface checks that require existing class changes
Flags or interface checks require modifying existing classes, violating OCP.Final Answer:
Option B -> Option BQuick Check:
Extend enums + new subclass + external map update -> minimal change [check]
Hint: Extend enums + subclass + external map -> minimal change [check]
Common Mistakes:
