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 the Online Shopping Cart system, the
Cart 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 controls instance creation, which matches the Singleton pattern.Step 2: Reason why Singleton fits Cart
Ensuring only one Cart instance per user session avoids inconsistent states and duplicate carts, which is the intended use case here.Final Answer:
Option B -> Option BQuick Check:
Private constructor + synchronized getInstance() -> Singleton pattern [check]
Hint: Private constructor + static getInstance() -> Singleton [check]
Common Mistakes:
3. The Car Rental System currently uses a
FeeStrategy interface for pricing. To add a WeekendFeeStrategy without modifying existing classes, which SOLID principle is being applied, and what is the minimum required change?medium
Solution
Step 1: Identify the SOLID principle for extensibility
Open/Closed Principle (OCP) states software entities should be open for extension but closed for modification.Step 2: Determine minimal change to add WeekendFeeStrategy
To avoid modifying existing classes, inject the new WeekendFeeStrategy via dependency injection, applying Dependency Inversion Principle (DIP) to depend on abstractions.Final Answer:
Option D -> Option DQuick Check:
Inject new strategy without modifying existing classes -> DIP [check]
Hint: Add new strategy via injection, not modifying existing code [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 the
Index class, without synchronizing the insertEntry() method, what exact sequence of Thread 1 and Thread 2 causes duplicate keys in the index?medium
Solution
Step 1: Identify the check-then-act race condition
Without synchronization, both threads can check for key existence simultaneously and see it as absent.Step 2: Trace thread interleaving causing duplicate insert
Thread 1 checks key -> false, context switch; Thread 2 checks key -> false; both proceed to insert the same key, causing duplicates.Final Answer:
Option A -> Option AQuick Check:
Concurrent check-then-act without sync -> duplicate keys inserted [check]
Hint: Check-then-act gap -> both see key absent -> duplicate insert [check]
Common Mistakes:
