Practice
Attendee entity be modeled as an abstract class or an interface, considering that attendees can be both internal employees and external guests with different data and behaviors?Solution
Step 1: Analyze state and behavior needs
Attendee has shared state (name, email) and some common behavior, which favors abstract class to avoid duplicating fields.Step 2: Evaluate interface limitations
Interfaces cannot hold state, so using interface would force duplication or external state holders, complicating design.Final Answer:
Option C -> Option CQuick Check:
Shared state + behavior -> abstract class [check]
WeekendFeeStrategy to calculate parking fees differently on weekends using the current adapter-based design. What is the minimum change required to comply with SOLID principles?Solution
Step 1: Identify SOLID principle involved
Open-Closed Principle (OCP) requires extending behavior without modifying existing code.Step 2: Evaluate options
Create a new WeekendFeeStrategy class implementing FeeStrategy and inject it where needed without modifying existing classes. respects OCP by adding a new class implementing FeeStrategy. Add a boolean flag to Vehicle to indicate weekend and modify ParkingLot to check it. modifies Vehicle and ParkingLot, violating OCP. Options A and D modify existing classes.Final Answer:
Option A -> Option AQuick Check:
New strategy class extends behavior without modifying existing code [check]
WeekendFeeStrategy to the existing pricing module of the in-memory SQL database's query cost estimation. Which minimum change respects the Open-Closed Principle (OCP)?Solution
Step 1: Understand OCP requirements
OCP requires software entities to be open for extension but closed for modification.Step 2: Evaluate options against OCP
Modify the existingFeeCalculatorclass to add weekend logic inside thecalculateFee()method. modifies existing class, violating OCP. Create a newWeekendFeeStrategyclass implementingFeeStrategyand inject it without changing existing classes. adds new strategy class without modifying existing code, respecting OCP. Add a booleanisWeekendflag to theFeeStrategyinterface and implement conditional logic in all strategies. adds flag to interface, forcing all implementations to change, violating OCP. Create a subclassWeekendFeeCalculatorextendingFeeCalculatorand overridecalculateFee(). subclasses but may require client changes, less flexible than strategy injection.Final Answer:
Option C -> Option CQuick Check:
New strategy class + injection -> OCP respected [check]
ConnectionManager, the method addConnection(User a, User b) checks if users are already connected before adding. Without synchronizing this method, what exact thread interleaving causes a double connection to be created?Solution
Step 1: Identify the check-then-act race condition
Without synchronization, both threads can see no existing connection and proceed to add, causing duplicates.Step 2: Trace exact thread interleaving
Thread 1 callsisConnected(a,b)and gets false, then context switches before adding. Thread 2 also callsisConnected(a,b)and gets false, then both add connections concurrently.Final Answer:
Option A -> Option AQuick Check:
Check-then-act gap causes double connection addition [check]
ElectricCar vehicles requiring ChargingSpot compatibility. What is the minimum change to the current design to add this without modifying existing classes?Solution
Step 1: Identify extensibility requirements
Adding ElectricCar and ChargingSpot must not modify existing classes to respect OCP and avoid class explosion.Step 2: Use enums and subclassing
Adding new enum values and a new ElectricCar subclass extends functionality cleanly; updating compatibility map centralizes spot-vehicle matching without changing existing code.Step 3: Avoid modifying base classes or Executor
Modifying Vehicle or Executor violates OCP and risks introducing bugs.Final Answer:
Option D -> Option DQuick Check:
New enums + subclass + compatibility map update -> minimal, no existing class change [check]
