Practice
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?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]
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?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]
WeekendFeeStrategy to apply extra charges on weekends. Given the current design uses a FeeStrategy interface for pricing, what is the minimum change required to add this feature while respecting SOLID principles?Solution
Step 1: Identify the SOLID principle involved
The Open-Closed Principle (OCP) requires that existing code should not be modified to add new behavior.Step 2: Evaluate options against OCP
Create a newWeekendFeeStrategyclass implementingFeeStrategyand inject it where needed without modifying existing classes. respects OCP by adding a new strategy class without modifying existing ones. Add a boolean weekend flag to theOrderclass and update all pricing logic accordingly. modifies Order class and pricing logic, violating OCP. Modify the existingFeeStrategyimplementation to add weekend logic with if-else checks. and D modify existing classes, violating OCP.Final Answer:
Option A -> Option AQuick Check:
New strategy class -> OCP respected, no existing class changes [check]
DeliveryStatus class has a method markDelivered() that updates the delivery state. Without synchronizing this method, which exact thread interleaving causes inconsistent delivery status updates?Solution
Step 1: Identify the race condition in markDelivered()
Without synchronization, two threads can read the undelivered status before either writes the delivered status.Step 2: Trace the interleaving causing lost update
Thread 1 reads undelivered -> context switch -> Thread 2 reads undelivered -> both write delivered, one overwriting the other's update.Final Answer:
Option A -> Option AQuick Check:
Check-then-act without sync -> lost update race condition [check]
ElectricCar and ChargingSpot without modifying existing classes?Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and subclasses allows extension without changing existing class code.Step 2: Update compatibility map externally to maintain open-closed principle and avoid class explosion.
Options B, C, and D require modifying existing classes, violating OCP or causing tight coupling.Final Answer:
Option A -> Option AQuick Check:
New enums + subclass + external compatibility update -> minimal change [check]
