Practice
1. In designing the Job Scheduler, should the
RetryPolicy be an abstract class or an interface, and why?easy
Solution
Step 1: Analyze RetryPolicy requirements
RetryPolicy typically holds state such as retry count, max retries, and backoff strategy, which benefits from shared code and fields.Step 2: Abstract class vs interface in Java
Abstract classes can hold state and provide default implementations, while interfaces (pre-Java 8) cannot hold state. Since RetryPolicy needs state, abstract class fits better.Final Answer:
Option D -> Option DQuick Check:
RetryPolicy holds state -> abstract class [check]
Hint: Stateful behavior -> abstract class; stateless -> interface [check]
Common Mistakes:
2. In an in-memory SQL database design, a
Table class has a private constructor and a static synchronized getInstance() method returning the single instance. Which pattern is applied here and why is this the correct use case?easy
Solution
Step 1: Identify the pattern from constructor and static method
A private constructor prevents external instantiation, and a static synchronizedgetInstance()method ensures controlled access to a single instance.Step 2: Match pattern intent to scenario
The Singleton pattern guarantees a single instance per JVM, suitable for a Table class representing a unique in-memory table. Factory Method and Builder focus on object creation but don't enforce single instance. Prototype clones objects, which is unrelated here.Final Answer:
Option D -> Option DQuick Check:
Private constructor + static synchronized getInstance() -> Singleton [check]
Hint: Private constructor + static getInstance() -> Singleton [check]
Common Mistakes:
3. In designing a
Vehicle abstraction for a parking system, should Vehicle be an abstract class or an interface if it needs to hold state like licensePlate and provide default behavior for getSize()? Why?easy
Solution
Step 1: Analyze state and behavior requirements
Vehicle needs to hold state (licensePlate) and provide default behavior (getSize), which interfaces cannot hold as fields.Step 2: Choose abstraction supporting state and default behavior
Abstract classes support instance variables and method implementations; interfaces cannot hold instance state.Final Answer:
Option A -> Option AQuick Check:
Abstract class supports state and default behavior -> correct choice [check]
Hint: State needs abstract class; interfaces can't hold instance fields [check]
Common Mistakes:
4. You want to add a
WeekendFeeStrategy to the traffic light system's parking fee calculation. According to SOLID principles, what is the minimum change required?medium
Solution
Step 1: Identify applicable SOLID principle
Open-Closed Principle (OCP) requires classes to be open for extension but closed for modification.Step 2: Evaluate options for OCP compliance
Adding a flag with conditional logic violates OCP by modifying existing class; creating a new strategy class and injecting it respects OCP.Final Answer:
Option A -> Option AQuick Check:
New strategy class + injection -> OCP compliant extension [check]
Hint: OCP: extend with new class, avoid modifying existing [check]
Common Mistakes:
5. To add a
WeekendFeeStrategy for locker usage fees in the current Amazon Locker design, which minimal change respects SOLID principles?medium
Solution
Step 1: Identify the SOLID principle involved
Open-Closed Principle (OCP) requires adding new behavior without modifying existing code.Step 2: Evaluate options for minimal change respecting OCP
Create a new WeekendFeeStrategy class implementing FeeStrategy and inject it without changing existing classes. suggests adding a new strategy class, but injecting it requires modifying existing FeeCalculator or client code, violating minimal change. Add a boolean isWeekend flag to Package and modify LockerManager to apply extra fees conditionally. adds a flag and conditional logic, which is a minimal change localized to Package and LockerManager, preserving OCP better than modifying FeeCalculator or Locker.Final Answer:
Option D -> Option DQuick Check:
Minimal change with conditional flag -> respects OCP [check]
Hint: OCP: add new behavior without modifying existing classes [check]
Common Mistakes:
