Practice
1. In the Amazon Locker Service design, a
LockerManager class has a private constructor and a static synchronized getInstance() method. Which design pattern is applied here and why is it the correct choice?easy
Solution
Step 1: Identify the pattern from constructor and method
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method ensures a single globally accessible instance.Step 2: Why Singleton fits this use case
LockerManager must coordinate locker assignments centrally to avoid conflicts, so a single instance is necessary. Factory or Builder do not enforce single instance, Prototype is for cloning, which is not needed here.Final Answer:
Option A -> Option AQuick Check:
Private constructor + synchronized getInstance() -> Singleton pattern [check]
Hint: Private constructor + static getInstance() -> Singleton [check]
Common Mistakes:
2. In the Online Stock Brokerage system, the
TradeManager class has a private constructor and a static synchronized getInstance() method. Which design pattern is applied here and why is this the correct choice?easy
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 this scenario
TradeManager must coordinate all trades globally to maintain consistent state; multiple instances would cause conflicting trade states.Final Answer:
Option D -> Option DQuick Check:
Private constructor + synchronized getInstance() -> Singleton pattern [check]
Hint: Private constructor + static synchronized getInstance() -> Singleton [check]
Common Mistakes:
3. In designing a
Vehicle abstraction, should it be an abstract class or an interface if it needs to hold state like speed and fuelLevel? Why?easy
Solution
Step 1: Analyze state and behavior requirements
SinceVehicleneeds to hold mutable state likespeedandfuelLevel, it requires fields and possibly default implementations that manage this state.Step 2: Compare abstract class vs interface capabilities
Abstract classes can hold instance variables and provide default method implementations that access state. Interfaces cannot hold instance state (only constants) and default methods cannot manage mutable state effectively.Final Answer:
Option C -> Option CQuick Check:
Stateful abstraction -> abstract class, not interface [check]
Hint: Stateful abstraction needs abstract class, not interface [check]
Common Mistakes:
4. In a vending machine using the State pattern, if the method
selectItem() is not synchronized, which exact thread interleaving causes dispensing the same item twice?medium
Solution
Step 1: Identify the check-then-act race
Without synchronization, both threads can read item availability as true before either decrements inventory.Step 2: Trace the interleaving
Thread 1 checks availability -> true, context switch; Thread 2 checks availability -> true; both proceed to dispense, causing double dispense.Final Answer:
Option A -> Option AQuick Check:
Unsynchronized check-then-act -> double dispense [check]
Hint: Check-then-act without sync -> double dispense race [check]
Common Mistakes:
5. In a multi-threaded Java implementation of a parking lot using the Template Method pattern, the
parkVehicle() method checks if a spot is available before assigning it. Without synchronization, which exact thread interleaving causes double-booking of the same spot?medium
Solution
Step 1: Trace check-then-act race
Thread 1 callsisAvailable()and sees true, but before assigning, context switches.Step 2: Thread 2 also calls
isAvailable()and sees true, then both assign the same spot concurrently.Final Answer:
Option D -> Option DQuick Check:
Unsynchronized check-then-act allows double assignment [check]
Hint: Check-then-act without sync -> double booking [check]
Common Mistakes:
