Practice
1. In designing the ESPNcricinfo system, should the
Ball entity be modeled as an abstract class or an interface? Consider that Ball has state like ballNumber and behavior like calculateRuns(). Which is the best choice and why?easy
Solution
Step 1: Analyze Ball's state and behavior
Ball has state (ballNumber) and behavior (calculateRuns), so it needs to store data and provide common logic.Step 2: Abstract class vs interface for state
Interfaces cannot hold instance state; abstract classes can hold state and provide shared implementation.Final Answer:
Option A -> Option AQuick Check:
Stateful entity with behavior -> abstract class [check]
Hint: Stateful with behavior -> abstract class, stateless behavior -> interface [check]
Common Mistakes:
2. In the LinkedIn OOP design, the
ConnectionManager class uses a private constructor and a static synchronized getInstance() method to provide a single instance. Which design pattern is applied here, and why is this the correct choice for managing user connections?easy
Solution
Step 1: Identify the pattern from constructor and method signature
The private constructor and static synchronizedgetInstance()method are classic signs of the Singleton pattern, ensuring a single instance.Step 2: Reason why Singleton fits connection management
Managing user connections globally requires a single shared instance to avoid inconsistent states or duplicated connection managers.Final Answer:
Option B -> Option BQuick Check:
Singleton ensures one global ConnectionManager instance [check]
Hint: Private constructor + static getInstance() -> Singleton [check]
Common Mistakes:
3. In the messaging system, the
DeliveryStatus class has a method markDelivered() that updates the delivery state. Without synchronizing this method, which exact thread interleaving causes inconsistent delivery status updates?medium
Solution
Step 1: Identify the race condition in markDelivered()
Without synchronization, two threads can read the undelivered status before either writes the delivered status.Step 2: Trace the interleaving causing lost update
Thread 1 reads undelivered -> context switch -> Thread 2 reads undelivered -> both write delivered, one overwriting the other's update.Final Answer:
Option A -> Option AQuick Check:
Check-then-act without sync -> lost update race condition [check]
Hint: Check-then-act gap -> lost update race [check]
Common Mistakes:
4. The system must support
ElectricCar vehicles needing ChargingSpot parking. 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, respecting OCP.Step 2: Update compatibility externally
UpdatingSpotCompatibilitymap outside existing classes avoids modifying their code, enabling new vehicle-spot matching.Final Answer:
Option D -> Option DQuick Check:
Extend enums + subclass + external map update -> no existing class changes [check]
Hint: Extend enums + subclass + external map -> no existing class changes [check]
Common Mistakes:
5. To support ElectricCar orders requiring ChargingSpots in the existing Order Management System, what is the minimum change that respects extensibility 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 options
Add ELECTRIC_CAR to VehicleType enum, CHARGING to SpotType enum, create ElectricCar subclass, and update SpotCompatibility map externally. adds new enum values and classes externally, updating compatibility map without changing existing code; Options B, C, D modify existing classes violating OCP.Final Answer:
Option C -> Option CQuick Check:
Adding new enums and classes externally respects OCP [check]
Hint: Add new enums and classes externally, no existing class changes [check]
Common Mistakes:
