Practice
1. In designing the
Player abstraction for the Snake and Ladder game, should Player be an abstract class or an interface, and why?easy
Solution
Step 1: Analyze state and behavior requirements
ThePlayerhas common state such as current position on the board and methods like move().Step 2: Choose abstraction type
An abstract class allows sharing state and default method implementations, which interfaces (pre-Java 8) cannot provide.Final Answer:
Option B -> Option BQuick Check:
Abstract class holds shared state and behavior [check]
Hint: State + default behavior -> abstract class, not interface [check]
Common Mistakes:
2. The auction system currently calculates fees with a fixed strategy. 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 SOLID principle
Open/Closed Principle (OCP) states software entities should be open for extension but closed for modification.Step 2: Apply OCP by adding WeekendFeeStrategy implementing FeeStrategy interface without modifying existing classes.
Final Answer:
Option A -> Option AQuick Check:
New strategy class + injection -> OCP compliance [check]
Hint: Add new strategy class, no existing code change -> OCP [check]
Common Mistakes:
3. To add a new
WeekendFeeStrategy for orders without modifying existing classes, which SOLID principle is best applied and what is the minimal required change?medium
Solution
Step 1: Identify SOLID principle for extension without modification
Open/Closed Principle (OCP) states classes should be open for extension but closed for modification.Step 2: Apply Dependency Inversion Principle (DIP) for injecting new strategy
Injecting WeekendFeeStrategy via constructor or setter respects DIP and allows Order to use new fee logic without internal changes.Final Answer:
Option A -> Option AQuick Check:
Inject new strategy -> no Order modification -> DIP + OCP [check]
Hint: Inject new strategy -> respect DIP and OCP [check]
Common Mistakes:
4. In the Stack Overflow voting system, if the
castVote() method is not synchronized, which exact sequence of thread interleaving causes a double vote count for the same user on a question?medium
Solution
Step 1: Identify the check-then-act race condition
The critical issue is the check if the user has already voted. Without synchronization, both threads see no vote recorded and proceed.Step 2: Trace exact interleaving causing double vote
Thread 1 checks voted -> false, context switch; Thread 2 checks voted -> false; both cast votes, causing double count.Final Answer:
Option B -> Option BQuick Check:
Check-then-act gap without sync -> double vote [check]
Hint: Check-then-act gap -> double vote without sync [check]
Common Mistakes:
5. ElectricCar vehicles require ChargingSpots in the parking module. What is the minimum change to the current Java design to support this without modifying existing classes?
hard
Solution
Step 1: Identify extensibility without modifying existing classes
Minimal change means no modification to existing classes; adding new enums or modifying compatibility map violates this.Step 2: Use composition or flags to extend behavior
Adding an isElectric boolean field to Vehicle and handling charging logic externally avoids class explosion and preserves OCP.Final Answer:
Option C -> Option CQuick Check:
Flag in Vehicle + external logic -> minimal change, no class modification [check]
Hint: Add flag + external logic -> minimal change, no class edits [check]
Common Mistakes:
