Practice
1. In the in-memory file system design, a
FileSystem class has a private constructor and a static synchronized getInstance() method returning the single instance. Which pattern is applied here and why is this the correct use case?easy
Solution
Step 1: Identify the pattern from constructor and method signature
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method ensures a single shared instance, matching Singleton pattern characteristics.Step 2: Reason why Singleton fits this use case
Singleton is appropriate because the file system state must be globally consistent and unique; multiple instances would cause conflicting states.Final Answer:
Option B -> Option BQuick Check:
Private constructor + static synchronized getInstance() -> Singleton [check]
Hint: Private constructor + static getInstance() -> Singleton [check]
Common Mistakes:
2. In designing the Order class, should the
OrderState be an abstract class or an interface? Consider that each state has different behavior but shares no common state variables. Which is the best choice and why?easy
Solution
Step 1: Analyze state sharing needs
States have no shared variables, only behavior differences, so no need for shared state in a superclass.Step 2: Evaluate interface vs abstract class
Interface suits behavior contracts without state and allows flexibility; abstract class is unnecessary and restrictive here.Final Answer:
Option B -> Option BQuick Check:
Interface fits stateless behavior contracts best [check]
Hint: Interface for behavior-only contracts without shared state [check]
Common Mistakes:
3. The Ride Booking system wants to add a
WeekendFeeStrategy for fare calculation. Given the current design uses a FeeStrategy interface, what is the minimum change required to comply with SOLID principles?medium
Solution
Step 1: Identify applicable SOLID principle
The Open-Closed Principle (OCP) requires classes to be open for extension but closed for modification.Step 2: Evaluate options against OCP
Create a new WeekendFeeStrategy class implementing FeeStrategy and inject it without modifying existing classes. adds a new FeeStrategy implementation without modifying existing classes, adhering to OCP. Options A and C modify existing classes, violating OCP. Create a WeekendFareCalculator subclass extending FareCalculator and override calculateFare(). creates subclass but may cause class explosion.Final Answer:
Option B -> Option BQuick Check:
New FeeStrategy implementation -> OCP compliant [check]
Hint: New strategy class, no existing class changes [check]
Common Mistakes:
4. In a parking lot system, the
parkVehicle() method checks if a spot is available and then marks it occupied. Without synchronized, which exact thread interleaving causes double-booking?medium
Solution
Step 1: Identify check-then-act race condition
Without synchronization, Thread 1 reads spot as available but is preempted before marking it occupied. Thread 2 also reads spot as available and proceeds to mark it occupied.Step 2: Understand resulting double-booking
Both threads believe the spot is free and park vehicles, causing double-booking due to the gap between check and update.Final Answer:
Option C -> Option CQuick Check:
Check-then-act gap without synchronization -> double-booking [check]
Hint: Check-then-act gap -> both see available -> double-book [check]
Common Mistakes:
5. The system needs to support
ElectricCar and ChargingSpot without modifying existing classes. What is the minimum change to the current Java design to add these features while preserving extensibility?hard
Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enums or modifying compatibility maps changes existing classes, violating OCP.Step 2: Use composition by adding an isElectric field and charging logic in ParkingSpot to handle charging without class explosion.
This approach extends functionality with minimal changes and avoids class explosion or god classes.Final Answer:
Option B -> Option BQuick Check:
Use fields and composition, avoid modifying enums or compatibility maps [check]
Hint: Add fields and logic, avoid modifying enums or compatibility maps [check]
Common Mistakes:
