Practice
1. The Meeting Scheduler 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 SOLID principle for extensibility
Open/Closed Principle (OCP) requires adding new behavior without modifying existing classes.Step 2: Apply Dependency Inversion Principle (DIP)
Injecting the new WeekendFeeStrategy via constructor or setter allows FeeCalculator to depend on abstractions, enabling extension without modification.Final Answer:
Option A -> Option AQuick Check:
Inject new strategy -> respects OCP and DIP [check]
Hint: Inject new strategy -> OCP + DIP respected [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 calculate parking fees differently on weekends using the current Iterator-based design. Which minimal change respects the Open-Closed Principle (OCP)?medium
Solution
Step 1: Understand OCP requirements
OCP requires software entities to be open for extension but closed for modification.Step 2: Evaluate options
Modify the existing FeeStrategy class to add weekend logic with an if-else condition. modifies existing class, violating OCP. Create a new WeekendFeeStrategy class implementing FeeStrategy and update the client code to use it when appropriate. adds a new class but requires client code modification, which may violate DIP. Add a boolean flagisWeekendto the FeeStrategy interface and implement conditional logic in all subclasses. extends interface with a flag, allowing existing implementations to adapt without changing their core logic. Combine all fee strategies into a single class with multiple methods and switch logic inside the iterator. centralizes logic, violating SRP and OCP.Final Answer:
Option A -> Option AQuick Check:
Extending interface with flag allows extension without modifying existing classes [check]
Hint: OCP: extend behavior without modifying existing classes [check]
Common Mistakes:
4. The system needs to support
ElectricCar and ChargingSpot without modifying existing classes. What is the minimum change to the current Java design to add these features while preserving extensibility?hard
Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enums or modifying compatibility maps changes existing classes, violating OCP.Step 2: Use composition by adding an isElectric field and charging logic in ParkingSpot to handle charging without class explosion.
This approach extends functionality with minimal changes and avoids class explosion or god classes.Final Answer:
Option B -> Option BQuick Check:
Use fields and composition, avoid modifying enums or compatibility maps [check]
Hint: Add fields and logic, avoid modifying enums or compatibility maps [check]
Common Mistakes:
5. To add
ElectricCar needing ChargingSpot without modifying existing classes, what is the minimum change to the current design using enums VehicleType and SpotType and a compatibility map?hard
Solution
Step 1: Understand current enum and compatibility map design
VehicleType and SpotType enums define types; compatibility map links them. Adding new types requires updating enums and map.Step 2: Analyze minimal change without modifying existing classes
AddELECTRIC_CARtoVehicleType,CHARGINGtoSpotType, createElectricCarsubclass, and update compatibility map; no existing classes modified adds new enum values and classes without modifying existing classes, respecting OCP. ModifyVehiclebase class to include charging capability and add logic in existing spot classes modifies base Vehicle class, violating OCP but minimal code change.Step 3: Choose minimal change that avoids modifying existing classes
AddELECTRIC_CARtoVehicleType,CHARGINGtoSpotType, createElectricCarsubclass, and update compatibility map; no existing classes modified is minimal and respects OCP; ModifyVehiclebase class to include charging capability and add logic in existing spot classes modifies existing classes, violating requirement.Final Answer:
Option A -> Option AQuick Check:
New enums + subclass + compatibility map update -> minimal change without existing class modification [check]
Hint: Add enums + subclass + map update; no existing class edits [check]
Common Mistakes:
