Practice
Solution
Step 1: Identify the role of the Doubly Linked List
The Doubly Linked List maintains the order of cache entries from most recently used to least recently used, enabling quick eviction of the least recently used item.Step 2: Match the pattern to this role
The Iterator pattern fits because it provides ordered traversal over the cache entries, allowing the system to efficiently move nodes and evict the least recently used.Final Answer:
Option A -> Option AQuick Check:
Doubly Linked List enables ordered traversal and eviction [check]
Vote class has a private constructor and a static synchronized getInstance() method. Which design pattern is applied here and why is this the correct use case?Solution
Step 1: Identify the pattern from constructor and method
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method ensures a single instance is created and accessed thread-safely.Step 2: Why Singleton fits Vote manager
Vote management requires a single point of control to maintain consistent vote counts and prevent conflicting updates, which Singleton enforces.Final Answer:
Option C -> Option CQuick Check:
Private constructor + synchronized static getInstance() -> Singleton [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]
FeeCalculator class. To add a WeekendFeeStrategy without modifying existing classes, which SOLID principle applies and what is the minimal correct change?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]
ElectricCar and ChargingSpot in the existing parking system using Decorator pattern. What is the minimum change to support this without modifying existing classes and avoiding class explosion?Solution
Step 1: Identify extensibility requirements
Adding ElectricCar and ChargingSpot should not modify existing classes to respect OCP and avoid class explosion.Step 2: Evaluate options for minimal change
AddELECTRIC_CARtoVehicleType,CHARGINGtoSpotType, createElectricCarextendingVehicle, and updateSpotCompatibility.COMPATIBILITY_MAPaccordingly. adds new enum values and a subclass, updating compatibility map without changing existing classes, adhering to OCP. ModifyVehicleandSpotclasses to include electric-specific fields and logic. modifies existing classes, violating OCP. Create separate parallel class hierarchy for electric vehicles and spots, duplicating existing logic. duplicates logic, causing class explosion. Add booleanisElectricflag toVehicleandSpotand handle logic with conditionals. adds flags and conditionals, increasing complexity and violating SRP.Final Answer:
Option B -> Option BQuick Check:
New enums and subclass added; existing classes unchanged [check]
