Practice
1. In implementing a chain of handlers in Java, should the Handler interface be an abstract class or an interface, and why?
easy
Solution
Step 1: Understand Handler role
Handlers define a contract to process or pass requests; they typically do not share mutable state.Step 2: Evaluate Java features
Interfaces allow multiple inheritance and default methods (since Java 8), promoting flexibility; abstract classes share state but restrict inheritance.Final Answer:
Option A -> Option AQuick Check:
Interface defines contract without forcing shared state [check]
Hint: Interface for contract, abstract class for shared state [check]
Common Mistakes:
2. In the Stack Overflow design, the
Vote class has a private constructor and a static synchronized getInstance() method. Which design pattern is applied here and why is this the correct use case?easy
Solution
Step 1: Identify the pattern from constructor and method
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method ensures a single instance is created and accessed thread-safely.Step 2: Why Singleton fits Vote manager
Vote management requires a single point of control to maintain consistent vote counts and prevent conflicting updates, which Singleton enforces.Final Answer:
Option C -> Option CQuick Check:
Private constructor + synchronized static getInstance() -> Singleton [check]
Hint: Private constructor + static synchronized getInstance() -> Singleton [check]
Common Mistakes:
3. In a multi-threaded parking lot system, if
parkVehicle() is not synchronized, which exact sequence of Thread 1 and Thread 2 calls causes double-booking of the same spot?medium
Solution
Step 1: Identify race condition
Without synchronization, both threads can read spot availability as true before either marks it occupied.Step 2: Trace exact interleaving
Thread 1: isAvailable() -> true, context switch; Thread 2: isAvailable() -> true; both proceed to park, causing double-booking.Final Answer:
Option A -> Option AQuick Check:
Check-then-act gap without synchronization -> double-booking [check]
Hint: Check availability then park must be atomic to avoid double-booking [check]
Common Mistakes:
4. You want to add a
WeekendFeeStrategy to calculate notification fees differently on weekends. According to SOLID principles, what is the minimum change required in the current design that uses a FeeStrategy interface?medium
Solution
Step 1: Identify applicable SOLID principle
The Open-Closed Principle (OCP) requires classes to be open for extension but closed for modification.Step 2: Evaluate options against OCP
Create a new WeekendFeeStrategy class implementing FeeStrategy and inject it without modifying existing classes. adds a new class implementing FeeStrategy without modifying existing code, adhering to OCP. Others modify existing classes or interfaces, violating OCP.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 [check]
Common Mistakes:
5. In a parking system,
parkVehicle() checks if a spot is available and then marks it occupied. Without synchronized, which exact thread interleaving causes double-booking?medium
Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads spot as available but is preempted before marking it occupied. Thread 2 also reads available and proceeds to mark occupied.Step 2: Confirm double-booking scenario
Both threads believe the spot is free and mark it occupied, causing double-booking due to the race between check and set.Final Answer:
Option B -> Option BQuick Check:
Check-then-act gap without synchronization -> double-booking [check]
Hint: Check-then-act without lock -> race condition double-booking [check]
Common Mistakes:
