Practice
Subject class maintains a list of Observer interfaces and notifies them on state changes. Which design pattern is this an example of, and why is it the correct choice for decoupling components?Solution
Step 1: Identify the pattern based on roles
TheSubjectmaintains a list ofObserverinterfaces and notifies them on state changes, which matches the Observer pattern's intent to decouple the subject from its observers.Step 2: Eliminate other patterns
Mediator centralizes communication but does not involve a subject notifying observers; Decorator adds behavior dynamically but does not handle notification; Strategy encapsulates algorithms but does not manage dependent objects.Final Answer:
Option A -> Option AQuick Check:
Subject notifying observers without knowing concrete classes -> Observer pattern [check]
FeeStrategy interface for scoring rules. To add a WeekendBonusStrategy that modifies scoring only on weekends, what is the minimum change required to comply with SOLID principles?Solution
Step 1: Identify applicable SOLID principle
The Open-Closed Principle (OCP) requires that existing classes remain unchanged when adding new functionality.Step 2: Evaluate options against OCP
Add a newWeekendBonusStrategyclass implementingFeeStrategyand configure it without changing existing classes. adds a new class implementing the interface, extending behavior without modifying existing code, thus respecting OCP.Final Answer:
Option D -> Option DQuick Check:
New class implements interface -> no existing class modified -> OCP [check]
bookSeat() method is not synchronized, which exact sequence of events between Thread 1 and Thread 2 causes double-booking of the same seat?Solution
Step 1: Trace Thread 1's check-then-act gap
Thread 1 reads seat as available but is preempted before marking it booked.Step 2: Thread 2 also reads seat as available and proceeds to book, causing double-booking.
Without synchronization, both threads act on stale availability state.Final Answer:
Option A -> Option AQuick Check:
Check-then-act gap without lock -> double-booking [check]
WeekendFeeStrategy for bounty fees in the current Stack Overflow design, which minimal change respects the Open/Closed Principle (OCP)?Solution
Step 1: Understand OCP in fee strategy context
OCP requires adding new behavior without modifying existing code.Step 2: Evaluate options for minimal change and OCP compliance
Add a boolean flagisWeekendtoBountyand handle fees internally adds a flag and internal logic, modifying existing class behavior, violating OCP. Create a newWeekendFeeStrategyclass implementingFeeStrategyand inject it without changing existing classes adds a new strategy class without modifying existing classes, respecting OCP. Modify the existingBountyFeeCalculatorclass to add weekend logic with if-else modifies existing class, violating OCP. Create a subclassWeekendBountyFeeCalculatorextendingBountyFeeCalculatorand override fee calculation creates subclass but may cause complexity and violates DIP if clients depend on concrete subclass.Final Answer:
Option A -> Option AQuick Check:
New strategy class + injection -> OCP respected [check]
WeekendFeeStrategy to calculate parking fees differently on weekends using the current Iterator-based design. Which minimal change respects the Open-Closed Principle (OCP)?Solution
Step 1: Understand OCP requirements
OCP requires software entities to be open for extension but closed for modification.Step 2: Evaluate options
Modify the existing FeeStrategy class to add weekend logic with an if-else condition. modifies existing class, violating OCP. Create a new WeekendFeeStrategy class implementing FeeStrategy and update the client code to use it when appropriate. adds a new class but requires client code modification, which may violate DIP. Add a boolean flagisWeekendto the FeeStrategy interface and implement conditional logic in all subclasses. extends interface with a flag, allowing existing implementations to adapt without changing their core logic. Combine all fee strategies into a single class with multiple methods and switch logic inside the iterator. centralizes logic, violating SRP and OCP.Final Answer:
Option A -> Option AQuick Check:
Extending interface with flag allows extension without modifying existing classes [check]
