Practice
1. In the auction system, should the
Bid entity be designed as an abstract class or an interface? Consider that bids have state (amount, bidder) and behavior (validate, notify). Which is the best choice and why?easy
Solution
Step 1: Analyze state and behavior needs
Bids have shared state (amount, bidder) and common behavior (validation, notification), which benefit from code reuse and encapsulation.Step 2: Compare abstract class vs interface
Abstract classes can hold state and provide default method implementations; interfaces cannot hold state and only define method signatures.Final Answer:
Option C -> Option CQuick Check:
Stateful entity with shared behavior -> abstract class [check]
Hint: State + behavior reuse -> abstract class, not interface [check]
Common Mistakes:
2. A class has a private constructor and a
clone() method that returns a copy of itself. Which design pattern is applied here and why is this the correct use case?easy
Solution
Step 1: Identify the pattern from constructor and clone()
The private constructor prevents direct instantiation, and the presence of aclone()method indicates object copying rather than new creation.Step 2: Match pattern intent to scenario
The Prototype pattern is used to clone objects when construction is expensive, which fits the scenario. Singleton restricts to one instance, Factory creates new instances, and Builder separates construction steps.Final Answer:
Option D -> Option DQuick Check:
Private constructor + clone() -> Prototype pattern [check]
Hint: Private constructor + clone() means Prototype pattern [check]
Common Mistakes:
3. You want to add a
WeekendRetryPolicy to the Job Scheduler without modifying existing classes. Which SOLID principle guides the minimum change, and what is the correct approach?medium
Solution
Step 1: Identify the SOLID principle for extensibility
Open/Closed Principle (OCP) states software entities should be open for extension but closed for modification.Step 2: Apply OCP to add WeekendRetryPolicy
Create a new class implementing RetryPolicy interface to add weekend-specific logic without modifying existing RetryPolicy or Job classes.Final Answer:
Option A -> Option AQuick Check:
New class implements interface -> no existing code changed [check]
Hint: OCP: extend by new class, don't modify existing [check]
Common Mistakes:
4. In a Singleton class without synchronized on
getInstance(), what EXACT sequence of Thread 1 and Thread 2 calls can cause two instances to be created?medium
Solution
Step 1: Identify the check-then-act race condition
Without synchronization, both threads can see instance == null before either creates it.Step 2: Trace thread interleaving
Thread 1 checks instance == null -> true, context switch before creating instance. Thread 2 also checks instance == null -> true, proceeds to create a second instance, causing double instantiation.Final Answer:
Option A -> Option AQuick Check:
Unsynchronized lazy init -> check-then-act race causes two instances [check]
Hint: Check-then-act without sync -> double instance creation [check]
Common Mistakes:
5. ElectricCar needs to be added to the product catalog with ChargingSpots. What is the minimum change to support this without modifying existing classes?
hard
Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and new classes for ElectricCar and ChargingSpot allows extension without changing existing class code, preserving OCP.Step 2: Why updating compatibility map is needed
SpotCompatibility.COMPATIBILITY_MAP controls which vehicles fit which spots; updating it ensures ElectricCar and ChargingSpot compatibility without modifying base classes.Final Answer:
Option B -> Option BQuick Check:
New enums + classes + compatibility map update -> minimal existing class changes [check]
Hint: Add enums + classes + map, no base class changes [check]
Common Mistakes:
