Practice
Piece abstraction for the Jigsaw Puzzle System, should Piece be an abstract class or an interface? Consider that each piece has state (shape, edges) and behavior (fit check). Which is the best choice and why?Solution
Step 1: Analyze state and behavior requirements
Pieces share common state (shape, edges) and behavior (fit check), so code reuse and shared state management is needed.Step 2: Evaluate abstract class vs interface
Abstract class allows holding common state and default behavior, while interface cannot hold state (pre-Java 8) and only defines contracts.Final Answer:
Option B -> Option BQuick Check:
Abstract class holds shared state and behavior for Pieces [check]
WeekendFeeStrategy to calculate parking fees differently on weekends. According to SOLID principles, what is the minimum change required in the current Abstract Factory design?Solution
Step 1: Identify SOLID principle involved
Open-Closed Principle (OCP) requires new functionality added via extension, not modification.Step 2: Evaluate options
Create a new WeekendFeeStrategy class implementing FeeStrategy and inject it without modifying existing classes. respects OCP by adding a new class without modifying existing ones. Add a boolean weekend flag to ParkingLot and modify fee calculation accordingly. modifies ParkingLot, violating OCP. Options A and D modify existing classes, violating OCP.Final Answer:
Option A -> Option AQuick Check:
New strategy class added, existing classes unchanged [check]
parkVehicle() is not synchronized, which exact thread interleaving causes double-booking of the same spot?Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads spot as available but before marking it occupied, context switches to Thread 2 which also reads available.Step 2: Both threads proceed to park, causing double-booking
Both threads believe the spot is free and park simultaneously, violating mutual exclusion.Final Answer:
Option A -> Option AQuick Check:
Check-then-act gap without synchronization -> double-booking [check]
ElectricCar vehicles that require ChargingSpot parking spots. What is the minimum change to the current design to support this without modifying existing classes?Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and new subclasses allows extension without changing existing class code.Step 2: Update compatibility map for new types
Updating SpotCompatibility.COMPATIBILITY_MAP supports new vehicle-spot matching without altering existing logic.Final Answer:
Option C -> Option CQuick Check:
Extend enums + new subclasses + update compatibility map -> no existing class changes [check]
ElectricCar support with ChargingSpot in the parking system. What is the minimum change to the current design to support this without modifying existing classes?Solution
Step 1: Identify extensibility requirements
Adding ElectricCar and ChargingSpot requires new types and compatibility rules without modifying existing classes to respect OCP and avoid class explosion.Step 2: Evaluate options
AddELECTRIC_CARtoVehicleTypeenum,CHARGINGtoSpotType, createElectricCarextendingVehicle, and updateSpotCompatibility.COMPATIBILITY_MAPaccordingly. adds new enum values and classes, updating compatibility map without changing existing code. ModifyVehicleclass to add a booleanisElectricand add charging logic inside existing methods. modifies Vehicle class, violating OCP. Create a newElectricVehicleinterface and modifyParkingLotto handle it specially. adds interface but requires modifying ParkingLot, violating minimal change. Add a god-classElectricCarManagerthat handles all electric car logic separately. creates god-class, violating SRP and maintainability.Final Answer:
Option A -> Option AQuick Check:
New enums + class + compatibility map update -> minimal change [check]
