Practice
1. In designing the
User and Message classes for the messaging system, should User be an abstract class or an interface, and why?easy
Solution
Step 1: Analyze if User has state or just behavior
User holds common state such as userId and username, which requires fields and possibly default implementations.Step 2: Determine if abstract class or interface fits state and behavior
Abstract class supports shared state and partial implementation, while interface cannot hold instance state.Final Answer:
Option C -> Option CQuick Check:
User has common state -> abstract class fits better than interface [check]
Hint: Stateful base class -> abstract class, stateless -> interface [check]
Common Mistakes:
2. In the ATM system, the
Transaction class uses a private constructor and exposes a static synchronized getInstance() method to create transaction objects. Which design pattern is applied here and why is it the correct choice?easy
Solution
Step 1: Identify the pattern from private constructor and static synchronized getInstance()
This is a classic Singleton pattern implementation ensuring only one instance per JVM with thread-safe lazy initialization.Step 2: Eliminate other patterns
Factory Method involves subclassing and delegation, not private constructors. Builder separates construction steps, unrelated here. Singleton pattern, because it ensures only one Transaction instance exists system-wide. is wrong because Singleton is about single instance, but synchronized getInstance() is the key for thread safety.Final Answer:
Option D -> Option DQuick Check:
Private constructor + synchronized getInstance() -> thread-safe Singleton [check]
Hint: Private constructor + synchronized getInstance() -> thread-safe Singleton [check]
Common Mistakes:
3. In a concurrent Job Scheduler, if the
scheduleJob() method is not synchronized, which exact thread interleaving causes the same Job to be scheduled twice?medium
Solution
Step 1: Identify the check-then-act race condition
Both threads check if the job is already scheduled before adding. Without synchronization, both see job absent.Step 2: Trace thread interleaving
Thread 1 checks -> false, context switch before adding; Thread 2 checks -> false, adds job; Thread 1 resumes and adds job again, causing double scheduling.Final Answer:
Option C -> Option CQuick Check:
Check-then-act gap without synchronization -> double scheduling [check]
Hint: Check-then-act gap -> double scheduling race [check]
Common Mistakes:
4. To support
ElectricCar needing ChargingSpot in the vending machine parking design, what is the minimum change that respects extensibility without modifying existing classes?hard
Solution
Step 1: Identify extensibility requirements
Adding new vehicle and spot types without modifying existing classes preserves OCP and avoids class explosion.Step 2: Evaluate options for minimal invasive change
AddELECTRIC_CARtoVehicleTypeenum,CHARGINGtoSpotTypeenum, createElectricCarsubclass, and updateSpotCompatibility.COMPATIBILITY_MAPaccordingly. adds new enum values and a subclass, updating compatibility map without changing existing classes. Others modify existing classes or add flags causing complexity.Final Answer:
Option B -> Option BQuick Check:
New enums and subclass extend design without modifying existing classes [check]
Hint: Add enums + subclass + compatibility map, no existing class changes [check]
Common Mistakes:
5. The ESPNcricinfo system needs to support
ElectricCar vehicles that require ChargingSpot parking spots. 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 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]
Hint: Extend enums + new subclasses + compatibility map -> minimal change [check]
Common Mistakes:
