Practice
1. In the Library Management System, the
ReservationManager 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
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 ReservationManager
ReservationManager must coordinate all reservations centrally to avoid conflicts; multiple instances would cause inconsistent state.Final Answer:
Option D -> Option DQuick Check:
Private constructor + static synchronized getInstance() -> Singleton pattern [check]
Hint: Private constructor + static getInstance() -> Singleton [check]
Common Mistakes:
2. In a Workflow Engine design, a
Step 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?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 shared instance, matching the Singleton pattern.Step 2: Why Singleton fits Workflow Engine Step
Singleton ensures consistent state across the workflow engine by having only one Step instance managing transitions, avoiding conflicting states.Final Answer:
Option A -> Option AQuick Check:
Private constructor + static synchronized getInstance() -> Singleton [check]
Hint: Private constructor + static synchronized method -> Singleton [check]
Common Mistakes:
3. In a multi-threaded parking system using the Decorator pattern, the
parkVehicle() method checks if a spot is available and then marks it occupied. Without synchronization, which exact thread interleaving causes double-booking?medium
Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads spot as available (true) but before marking it occupied, context switches.Step 2: Trace Thread 2's actions
Thread 2 also reads spot as available (true) and proceeds to mark it occupied. Both threads think the spot is free and park vehicles, causing double-booking.Final Answer:
Option A -> Option AQuick Check:
Check-then-act without sync allows both threads to park simultaneously [check]
Hint: Check-then-act gap causes double-booking without sync [check]
Common Mistakes:
4. In the Ride Booking system, the
parkVehicle() method checks if a parking spot is available and then marks it occupied. Without synchronization, which exact thread interleaving causes double-booking of the same spot?medium
Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads spot as free (isAvailable() -> true) but is preempted before marking it occupied.Step 2: Thread 2 also reads spot as free and proceeds to mark it occupied, causing double-booking.
This is the classic check-then-act gap causing race conditions.Final Answer:
Option A -> Option AQuick Check:
Unsynchronized check-then-act -> double-booking race [check]
Hint: Check-then-act without sync -> double-booking race [check]
Common Mistakes:
5. You need to add support for
ElectricCar and ChargingSpot in your adapter-based parking system. What is the minimum change to the current design to support this without modifying existing classes?hard
Solution
Step 1: Identify extensibility requirements
New vehicle and spot types must be added without modifying existing classes to respect OCP.Step 2: Evaluate Java-specific implementation
Adding new enum constants and new classes extends functionality cleanly; updating compatibility map centralizes logic without class changes.Step 3: Reject options modifying existing classes
Options B, C, and D modify existing classes or add flags, violating OCP and risking class explosion.Final Answer:
Option A -> Option AQuick Check:
New enums and classes added, existing classes unchanged [check]
Hint: Add enums and classes, avoid modifying existing ones [check]
Common Mistakes:
