Practice
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.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]
Match class has a private constructor and a static synchronized getInstance() method to ensure only one instance per match ID exists. Which design pattern is applied here and why is this the correct use case?Solution
Step 1: Identify the pattern from constructor and getInstance()
The private constructor prevents direct instantiation, and the static synchronized getInstance() ensures controlled, thread-safe instance creation.Step 2: Why Singleton fits this use case
Singleton ensures only one Match instance per match ID exists, avoiding inconsistent state across threads.Final Answer:
Option A -> Option AQuick Check:
Private constructor + synchronized getInstance() -> Singleton pattern [check]
WeekendBonusRule that gives players extra moves on weekends without modifying existing classes. Which SOLID principle guides the minimal change, and what is the correct approach?Solution
Step 1: Identify SOLID principle for extension without modification
Open/Closed Principle (OCP) states software entities should be open for extension but closed for modification.Step 2: Evaluate approaches
Subclassing Player to WeekendPlayer allows extension without modifying Player; injecting new MoveRule class also respects OCP but requires interface design.Final Answer:
Option C -> Option CQuick Check:
Subclassing extends behavior without modifying base class [check]
Solution
Step 1: Identify the SOLID principle for extensibility
Open/Closed Principle (OCP) states that software entities should be open for extension but closed for modification.Step 2: Analyze the approach
Implementing an EvictionPolicy interface and injecting it into the cache decouples cache from eviction logic, following Dependency Inversion Principle (DIP), which is key here.Final Answer:
Option B -> Option BQuick Check:
Cache depends on abstraction, not concrete eviction -> DIP [check]
Solution
Step 1: Identify extensibility requirements
Adding ElectricCar and ChargingSpot must avoid modifying existing classes to respect OCP.Step 2: Evaluate options for minimal invasive change
Add ELECTRIC_CAR to VehicleType enum, CHARGING to SpotType enum, create ElectricCar subclass, and update SpotCompatibility map externally adds new enum values and subclasses externally, updating compatibility map without changing existing code. Options B and C modify base classes, violating OCP. Create a god-class that handles both ElectricCar and ChargingSpot logic to avoid modifying existing classes creates god-class, violating SRP and maintainability.Final Answer:
Option C -> Option CQuick Check:
New enums + subclass + external map update -> minimal change, OCP respected [check]
