Practice
Transaction 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 this the correct use case?Solution
Step 1: Identify the pattern from constructor and method signatures
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 this Wallet Transaction scenario
Singleton is appropriate when exactly one instance is needed to coordinate actions or maintain global state, such as a ledger manager or transaction coordinator. Factory Method and Builder patterns do not enforce single instance, and Prototype is for cloning, not restricting instances.Final Answer:
Option B -> Option BQuick Check:
Private constructor + synchronized static getInstance() -> Singleton pattern [check]
parkVehicle() is not synchronized, which exact thread interleaving causes double-booking of the same spot?Solution
Step 1: Identify race condition in parkVehicle()
Without synchronization, two threads can both check spot availability before either marks it occupied.Step 2: Trace exact interleaving
Thread 1 calls isAvailable() -> true, context switch before marking occupied; Thread 2 calls isAvailable() -> true; both proceed to park, causing double-booking.Final Answer:
Option B -> Option BQuick Check:
Check-then-act gap without synchronization causes double-booking [check]
KitchenQueue class has a parkOrder() method that checks if a slot is available before assigning an order. Without synchronization on parkOrder(), which exact thread interleaving causes double-booking of the same slot?Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads slot as available but is preempted before assignment; Thread 2 also reads available and proceeds.Step 2: Both threads assign the same slot leading to double-booking
Because the availability check and assignment are not atomic, both threads assign the same slot concurrently.Final Answer:
Option B -> Option BQuick Check:
Check-then-act gap without sync -> double assignment [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 that requires ChargingSpot compatibility in the current parking system. What is the minimum change to the design to support this without modifying existing classes?Solution
Step 1: Identify extensibility requirements
ElectricCar and ChargingSpot are new types requiring compatibility mapping without changing existing classes.Step 2: Apply minimal invasive change
Adding new enum values and subclass plus updating compatibility map extends functionality without modifying existing class code, preserving OCP.Step 3: Avoid violating OCP
Options B, C, and D modify existing classes or create parallel hierarchies, increasing complexity and violating OCP.Final Answer:
Option B -> Option BQuick Check:
Extend enums and map, subclass ElectricCar -> minimal change [check]
