Practice
1. In the Elevator System design, the
ElevatorController class uses a private constructor and a static synchronized getInstance() method to provide a single global access point. Which design pattern is applied here and why is it the correct choice?easy
Solution
Step 1: Identify Singleton usage
The private constructor and static synchronizedgetInstance()method restrict instantiation to one object, ensuring a single ElevatorController.Step 2: Why Singleton fits ElevatorController
ElevatorController coordinates multiple elevators and floor queues centrally, so multiple instances would cause inconsistent state and scheduling conflicts.Final Answer:
Option B -> Option BQuick Check:
Singleton ensures one global ElevatorController instance [check]
Hint: Singleton restricts to one instance for centralized control [check]
Common Mistakes:
2. 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:
3. The current Tic-Tac-Toe design uses a
WinStrategy interface for win detection. To add a WeekendWinBonusStrategy that applies only on weekends, what is the minimum change required to comply with SOLID principles?medium
Solution
Step 1: Understand OCP and DIP in current design
Open-Closed Principle (OCP) requires adding new behavior without modifying existing classes. Dependency Inversion Principle (DIP) encourages depending on abstractions.Step 2: Evaluate options for minimal change
Create a newWeekendWinBonusStrategyclass implementingWinStrategyand use it without modifying existing classes. adds a new strategy class but requires changing client code to use it, violating minimal change. Add a boolean flag in theBoardclass to toggle weekend mode and update all win checks accordingly. adds a toggle flag in Board, allowing existing strategies to adapt behavior without modifying their code, preserving OCP better than A or D.Final Answer:
Option B -> Option BQuick Check:
Toggle flag in Board -> minimal change, respects OCP [check]
Hint: OCP: add new class or toggle flag, avoid modifying existing [check]
Common Mistakes:
4. You need to add support for
ElectricCar that requires ChargingSpot compatibility in the current parking system. What is the minimum change to the design to support this without modifying existing classes?hard
Solution
Step 1: Identify extensibility requirements
ElectricCar and ChargingSpot are new types requiring compatibility mapping without changing existing classes.Step 2: Apply minimal invasive change
Adding new enum values and subclass plus updating compatibility map extends functionality without modifying existing class code, preserving OCP.Step 3: Avoid violating OCP
Options B, C, and D modify existing classes or create parallel hierarchies, increasing complexity and violating OCP.Final Answer:
Option B -> Option BQuick Check:
Extend enums and map, subclass ElectricCar -> minimal change [check]
Hint: Extend enums and map, subclass new type -> minimal change [check]
Common Mistakes:
5. You need to add support for
ElectricCar and ChargingSpot in the parking system without modifying existing classes. What is the minimum change to the current design?hard
Solution
Step 1: Identify extensibility without modifying existing classes
Enums can be extended with new constants, and new subclasses can be added without changing existing code.Step 2: Use external compatibility map update
Updating the compatibility map externally avoids modifying existing classes, preserving OCP and avoiding class explosion.Step 3: Avoid flags or interface checks that require existing class changes
Flags or interface checks require modifying existing classes, violating OCP.Final Answer:
Option B -> Option BQuick Check:
Extend enums + new subclass + external map update -> minimal change [check]
Hint: Extend enums + subclass + external map -> minimal change [check]
Common Mistakes:
