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]
Cart 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 controls instance creation, which matches the Singleton pattern.Step 2: Reason why Singleton fits Cart
Ensuring only one Cart instance per user session avoids inconsistent states and duplicate carts, which is the intended use case here.Final Answer:
Option B -> Option BQuick Check:
Private constructor + synchronized getInstance() -> Singleton pattern [check]
WeekendBonusRule that gives players extra moves on weekends without modifying existing classes. Which SOLID principle guides the minimal change, and what is the correct approach?Solution
Step 1: Identify SOLID principle for extension without modification
Open/Closed Principle (OCP) states software entities should be open for extension but closed for modification.Step 2: Evaluate approaches
Subclassing Player to WeekendPlayer allows extension without modifying Player; injecting new MoveRule class also respects OCP but requires interface design.Final Answer:
Option C -> Option CQuick Check:
Subclassing extends behavior without modifying base class [check]
ChargingSpot compatibility in the Jigsaw Puzzle System. What is the minimum design change to support ElectricCar and ChargingSpot without modifying existing classes?Solution
Step 1: Identify extensibility requirements
New piece and spot types must be added without modifying existing classes to respect OCP.Step 2: Evaluate minimal change approach
Adding new enum values and subclass for ElectricCarPiece plus updating compatibility map externally allows extension without modifying existing code.Final Answer:
Option A -> Option AQuick Check:
New enums and subclass extend system without changing existing classes [check]
ElectricCar and ChargingSpot support without modifying existing classes, what is the minimum change to the current Java design?Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and new subclasses allows extension without changing existing class code, respecting OCP.Step 2: Use compatibility map update for new spot-vehicle matching
Updating SpotCompatibility.COMPATIBILITY_MAP with new types supports new logic without modifying existing classes' internals.Final Answer:
Option D -> Option DQuick Check:
New enums + subclasses + compatibility map update -> minimal change, no existing class modification [check]
