Practice
1. In the Ride Booking system, the
FareCalculator 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?easy
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 shared instance, matching Singleton pattern characteristics.Step 2: Why Singleton fits FareCalculator
FareCalculator needs consistent fare rules and shared state (e.g., pricing configs). Singleton avoids multiple inconsistent instances and ensures thread-safe access.Final Answer:
Option C -> Option CQuick Check:
Private constructor + synchronized getInstance() -> Singleton pattern [check]
Hint: Private constructor + static getInstance() = Singleton [check]
Common Mistakes:
2. A Java class has a private constructor and a static synchronized
getInstance() method returning the single instance. Which design pattern is applied and why is this the CORRECT use case?easy
Solution
Step 1: Identify the pattern by constructor and method
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method controls access to a single instance, matching Singleton pattern characteristics.Step 2: Eliminate other patterns
Factory creates multiple instances, Builder constructs complex objects with multiple steps, and Prototype clones objects; none restrict to a single instance.Final Answer:
Option D -> Option DQuick Check:
Private constructor + synchronized static getter -> Singleton pattern [check]
Hint: Private constructor + static synchronized getter -> Singleton [check]
Common Mistakes:
3. In a multi-threaded inventory update method
updateStock() without synchronization, which exact sequence causes a race condition leading to incorrect stock count?medium
Solution
Step 1: Trace the check-then-act gap
Thread 1 reads stock=10 but is preempted before writing update. Thread 2 reads the same stock=10 and writes stock=15. Thread 1 resumes and overwrites with stock=15, losing Thread 2's update.Step 2: Why other options fail
Thread 1 reads stock=10, Thread 2 reads stock=10, both add 5, final stock=15 incorrectly assumes final stock=15 after both add 5 (should be 20). Thread 1 writes stock=15, Thread 2 reads stock=15, then writes stock=20 correctly shows correct sequence with no race. Thread 1 and Thread 2 both wait on a lock, preventing race conditions describes locking, which is absent here.Final Answer:
Option D -> Option DQuick Check:
Unsynchronized read-update-write -> lost update race condition [check]
Hint: Unsynchronized read-update-write causes lost updates [check]
Common Mistakes:
4. The system needs to support
ElectricCar playback devices that require ChargingSpot management. What is the minimum change to the current design to support this without modifying existing classes?hard
Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and subclassing allows extension without changing existing class code.Step 2: Update compatibility externally
Updating SpotCompatibility map outside existing classes respects OCP and avoids class explosion or god-class logic.Final Answer:
Option A -> Option AQuick Check:
Extend enums + subclass + external map update -> minimal change [check]
Hint: Extend enums + subclass + external map -> minimal change [check]
Common Mistakes:
5. To add an
ElectricPlayer who requires ChargingSpot in the Snake and Ladder game, what is the minimum change to the current design to maintain extensibility without modifying existing classes?hard
Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and new subclass preserves existing class code unchanged.Step 2: Update compatibility map for new player and spot types
SpotCompatibility map extension allows new rules without changing existing logic.Final Answer:
Option A -> Option AQuick Check:
New enums + subclass + compatibility map extension -> minimal existing code change [check]
Hint: New enums + subclass + map update = extensible addition [check]
Common Mistakes:
