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. The Car Rental System currently uses a
FeeStrategy interface for pricing. To add a WeekendFeeStrategy without modifying existing classes, which SOLID principle is being applied, and what is the minimum required change?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: Determine minimal change to add WeekendFeeStrategy
To avoid modifying existing classes, inject the new WeekendFeeStrategy via dependency injection, applying Dependency Inversion Principle (DIP) to depend on abstractions.Final Answer:
Option D -> Option DQuick Check:
Inject new strategy without modifying existing classes -> DIP [check]
Hint: Add new strategy via injection, not modifying existing code [check]
Common Mistakes:
3. To add a
WeekendFeeStrategy for weekend pricing in the current booking system design, which minimal change respects the Open-Closed Principle (OCP)?medium
Solution
Step 1: Identify OCP requirement
OCP requires adding new functionality without modifying existing tested code.Step 2: Evaluate options
Add a boolean flagisWeekendinBookingand handle fees there. adds weekend flag in Booking, which violates OCP by modifying Booking class. Create a new class implementingFeeStrategyinterface for weekend fees and inject it without changing existing classes. creates a new FeeStrategy class, respecting OCP by extension. Modify the existingFeeCalculatorclass to add weekend logic with if-else conditions. and D modify existing classes, violating OCP.Final Answer:
Option C -> Option CQuick Check:
New FeeStrategy class -> extend without modifying existing code [check]
Hint: New strategy class -> extend, don't modify [check]
Common Mistakes:
4. In the Facebook design, the
FriendRequestManager class has a sendRequest() method that checks if a friend request exists before creating one. Without synchronization, which exact thread interleaving causes duplicate friend requests?medium
Solution
Step 1: Identify the check-then-act race condition
Both threads check for existing request before creation; without synchronization, both see none.Step 2: Trace thread interleaving
Thread 1 checks and pauses before creating; Thread 2 checks and proceeds; both create duplicate requests.Final Answer:
Option D -> Option DQuick Check:
Unsynchronized check-then-act -> duplicate friend requests [check]
Hint: Check-then-act without lock -> race condition [check]
Common Mistakes:
5. In a parking lot system, if
parkVehicle() is not synchronized, which exact sequence of Thread 1 and Thread 2 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 available but before marking it occupied, context switches to Thread 2 which also reads it as available.Step 2: Both threads proceed to park, causing double-booking
Because the state update is not atomic, both threads park vehicles in the same spot simultaneously.Final Answer:
Option C -> Option CQuick Check:
Unsynchronized check-then-act -> double-booking race [check]
Hint: Check-then-act gap causes double-booking without synchronization [check]
Common Mistakes:
