Practice
1. In designing the
Piece abstraction for the Jigsaw Puzzle System, should Piece be an abstract class or an interface? Consider that each piece has state (shape, edges) and behavior (fit check). Which is the best choice and why?easy
Solution
Step 1: Analyze state and behavior requirements
Pieces share common state (shape, edges) and behavior (fit check), so code reuse and shared state management is needed.Step 2: Evaluate abstract class vs interface
Abstract class allows holding common state and default behavior, while interface cannot hold state (pre-Java 8) and only defines contracts.Final Answer:
Option B -> Option BQuick Check:
Abstract class holds shared state and behavior for Pieces [check]
Hint: State + behavior = abstract class, not interface [check]
Common Mistakes:
2. You want to add a
WeekendFeeStrategy to calculate parking fees differently on weekends using the current adapter-based design. What is the minimum change required to comply with SOLID principles?medium
Solution
Step 1: Identify SOLID principle involved
Open-Closed Principle (OCP) requires extending behavior without modifying existing code.Step 2: Evaluate options
Create a new WeekendFeeStrategy class implementing FeeStrategy and inject it where needed without modifying existing classes. respects OCP by adding a new class implementing FeeStrategy. Add a boolean flag to Vehicle to indicate weekend and modify ParkingLot to check it. modifies Vehicle and ParkingLot, violating OCP. Options A and D modify existing classes.Final Answer:
Option A -> Option AQuick Check:
New strategy class extends behavior without modifying existing code [check]
Hint: OCP: extend with new class, don't modify existing ones [check]
Common Mistakes:
3. In the Library Management System, the
reserveBook() method is not synchronized. Which exact sequence of Thread 1 and Thread 2 calls causes double reservation of the same book copy?medium
Solution
Step 1: Identify the check-then-act race condition
Without synchronization, both threads can read availability as true before either updates the reservation status.Step 2: Trace thread interleaving causing double booking
Thread 1 reads available=true, context switch; Thread 2 reads available=true; both proceed to reserve, causing double booking.Final Answer:
Option A -> Option AQuick Check:
Unsynchronized check-then-act -> both see available=true -> double reservation [check]
Hint: Check availability -> context switch -> both reserve -> double booking [check]
Common Mistakes:
4. In the auction system, the
placeBid() method checks if the auction is still open before accepting a bid. Without synchronizing placeBid(), which exact thread interleaving causes two threads to accept bids after the auction closes, resulting in inconsistent state?medium
Solution
Step 1: Identify check-then-act race
Without synchronization, Thread 1 reads auction open as true but is preempted before recording the bid.Step 2: Thread 2 reads auction open as true and proceeds to record bid as well, both bypassing the closed state check.
Final Answer:
Option D -> Option DQuick Check:
Unsynchronized check-then-act -> double acceptance after close [check]
Hint: Check-then-act without sync -> race condition double accept [check]
Common Mistakes:
5. The current Facebook design uses a
PrivacyStrategy interface for post visibility. To add a WeekendPrivacyStrategy that restricts posts only on weekends, what is the minimum change required to comply with SOLID principles?medium
Solution
Step 1: Identify applicable SOLID principle
Open/Closed Principle (OCP) requires extending behavior without modifying existing code.Step 2: Apply OCP to privacy strategies
Adding a new class implementingPrivacyStrategyextends functionality without changing existing classes.Final Answer:
Option B -> Option BQuick Check:
New class for new behavior -> OCP compliance [check]
Hint: OCP: extend with new class, don't modify existing [check]
Common Mistakes:
