Practice
1. A
RateLimiter class has a private constructor and a static synchronized getInstance() method returning a single instance. Which design pattern is applied and why is this the correct choice for a token bucket rate limiter?easy
Solution
Step 1: Identify the pattern from constructor and getInstance()
The private constructor and static synchronized getInstance() method indicate Singleton pattern, ensuring only one instance exists.Step 2: Why Singleton fits token bucket rate limiter
Singleton ensures a single shared token bucket state across threads, preventing inconsistent token counts.Final Answer:
Option C -> Option CQuick Check:
Singleton enforces single shared instance for thread-safe token bucket [check]
Hint: Private constructor + static synchronized getInstance() -> Singleton [check]
Common Mistakes:
2. In designing a
TokenBucketRateLimiter class, should the RateLimiter be an abstract class or an interface? Consider that the rate limiter maintains internal token state and exposes tryConsume() behavior.easy
Solution
Step 1: Analyze state and behavior requirements
RateLimiter needs to maintain internal token count state and provide tryConsume() behavior.Step 2: Abstract class vs interface for stateful behavior
Abstract class can hold shared mutable state and provide partial implementation; interfaces cannot hold instance state.Final Answer:
Option A -> Option AQuick Check:
Abstract class supports shared token state and behavior [check]
Hint: Stateful base class -> abstract class, not interface [check]
Common Mistakes:
3. You want to add a new WeekendFeeStrategy to the existing chain of responsibility-based parking fee system. Which change respects the Open-Closed Principle (OCP) and Dependency Inversion Principle (DIP)?
medium
Solution
Step 1: Understand OCP and DIP
OCP requires extending behavior without modifying existing code; DIP requires depending on abstractions, not concretions.Step 2: Evaluate options
Modify the existing FeeHandler class to add weekend fee logic inside its handleRequest() method. modifies existing class, violating OCP; Create a new WeekendFeeHandler implementing the FeeStrategy interface and add it to the chain without changing existing handlers. adds new handler, respecting OCP and DIP; Add a boolean flag isWeekend to FeeHandler and branch logic based on it. adds flag inside existing handler, violating OCP; Create a subclass WeekendFeeHandler extending FeeHandler and override handleRequest() with weekend logic. subclasses but may cause tight coupling and violates DIP if chain depends on concrete classes.Final Answer:
Option D -> Option DQuick Check:
New handler class added without modifying existing code [check]
Hint: Add new handler class, don't modify existing ones [check]
Common Mistakes:
4. You want to add a
WeekendFeeStrategy to calculate message delivery fees differently on weekends. According to SOLID principles, what is the minimum change required in the current design?medium
Solution
Step 1: Identify the SOLID principle involved
Open-Closed Principle (OCP) requires adding new behavior without modifying existing classes.Step 2: Evaluate options against OCP
Add a boolean flagisWeekendinMessageand handle fees there. adds weekend logic inside Message, violating SRP and mixing concerns; Modify the existingFeeCalculatorclass to add weekend logic with an if-else block. modifies existing class, violating OCP; Create a subclassWeekendFeeCalculatorextendingFeeCalculatorand override fee calculation. creates subclass but may cause class explosion; Create a newWeekendFeeStrategyclass implementing the existing FeeStrategy interface and inject it where needed. adds new strategy class implementing FeeStrategy, adhering to OCP and DIP.Final Answer:
Option A -> Option AQuick Check:
New strategy class -> OCP and DIP respected [check]
Hint: New strategy class -> Open-Closed Principle [check]
Common Mistakes:
5. Adding a
WeekendFeeStrategy to calculate weekend parking fees requires which minimum change to the current design that uses a FeeStrategy interface?medium
Solution
Step 1: Understand current design with FeeStrategy interface
FeeStrategy interface allows different fee calculation implementations, supporting OCP if new strategies are added as new classes.Step 2: Analyze minimal change for weekend fees
Create a newWeekendFeeStrategyclass implementingFeeStrategyand inject it without modifying existing classes respects OCP by adding a new class without modifying existing ones. Add a boolean flagisWeekendinParkingLotand conditionally apply fees inside existing strategies modifies ParkingLot to add weekend logic, violating OCP but minimal change.Final Answer:
Option D -> Option DQuick Check:
Minimal change but violates OCP -> modify ParkingLot with weekend flag [check]
Hint: Minimal change โ OCP compliance; adding flag is minimal but not ideal [check]
Common Mistakes:
