Practice
Playlist class has a private constructor and a static synchronized getInstance() method that returns the single instance. Which design pattern is applied here and why is it the correct choice?Solution
Step 1: Identify the pattern from private constructor and static synchronized getInstance()
This is the Singleton pattern, which restricts instantiation to one object and provides a global access point.Step 2: Why Singleton fits Playlist playback queue management
Since the playback queue must be globally consistent and unique per user session, Singleton ensures only one instance manages it, avoiding conflicting states.Final Answer:
Option D -> Option DQuick Check:
Private constructor + synchronized getInstance() -> Singleton pattern [check]
ParkingLot class has a private constructor and a static synchronized getInstance() method returning the single instance. 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 globally.Step 2: Why Singleton fits ParkingLot
ParkingLot must be unique to coordinate spot allocation and pricing consistently across the system, so Singleton enforces this single shared instance.Final Answer:
Option D -> Option DQuick Check:
Private constructor + synchronized static getInstance() -> Singleton pattern [check]
tryConsume() method in TokenBucketRateLimiter that checks if tokens are available and then decrements the count without synchronization. Which exact thread interleaving causes double token consumption?Solution
Step 1: Identify check-then-act race condition
Without synchronization, both threads can read tokens > 0 before either decrements.Step 2: Trace exact interleaving causing double consumption
Thread 1 checks tokens > 0 -> true, context switch; Thread 2 checks tokens > 0 -> true; both proceed to decrement, consuming same token twice.Final Answer:
Option D -> Option DQuick Check:
Unsynchronized check-then-act allows double token consumption [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 needing ChargingSpot compatibility, what is the minimum change to the current design without modifying existing classes?Solution
Step 1: Identify extensibility without modifying existing classes
Adding new vehicle and spot types via enums and creating a new subclassElectricCarextends functionality without changing existing code.Step 2: Update compatibility map externally
UpdatingSpotCompatibility.COMPATIBILITY_MAPto include new mappings allows the system to recognize charging spots without modifying core logic.Final Answer:
Option C -> Option CQuick Check:
New enums + subclass + compatibility map update -> minimal change, OCP preserved [check]
