Practice
1. 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:
2. In a vending machine design, a
State interface defines methods like selectProduct(), insertMoney(), and dispense(). Concrete classes like IdleState, HasMoneyState, and SoldState implement this interface. Which design pattern is applied here and why is it the correct choice?easy
Solution
Step 1: Identify the role of State interface and concrete states
TheStateinterface defines behavior that varies depending on the vending machine's current state. Concrete classes implement these behaviors and handle transitions.Step 2: Match pattern intent to scenario
The State pattern encapsulates state-specific behavior and transitions, allowing the vending machine to change behavior dynamically without complex conditionals.Final Answer:
Option D -> Option DQuick Check:
State pattern encapsulates state behavior and transitions [check]
Hint: State pattern encapsulates behavior per state [check]
Common Mistakes:
3. You want to add a
WeekendFeeStrategy to calculate parking fees differently on weekends. Given the current design uses a FeeStrategy interface, what is the minimum change to comply with SOLID principles?medium
Solution
Step 1: Identify SOLID principle
Open-Closed Principle (OCP) requires classes to be open for extension but closed for modification.Step 2: Apply OCP
Creating a newWeekendFeeStrategyclass extends functionality without modifying existing code, adhering to OCP and Dependency Inversion Principle.Final Answer:
Option C -> Option CQuick Check:
New strategy class -> extend without modifying existing -> OCP [check]
Hint: Add new strategy class, don't modify existing code [check]
Common Mistakes:
4. In the ParkingLot class, if
parkVehicle() is not synchronized, which exact sequence of Thread 1 and Thread 2 causes double-booking of the same spot?medium
Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads spot as available but before marking it occupied, Thread 2 also reads it as available.Step 2: Both threads proceed to assign the same spot concurrently
Because no lock protects the check and assignment, both parkVehicle calls succeed, causing double-booking.Final Answer:
Option A -> Option AQuick Check:
Unsynchronized check-then-act -> double-booking race [check]
Hint: Check-then-act without lock -> double-booking race [check]
Common Mistakes:
5. You want to add a
WeekendFeeStrategy to calculate weekend parking fees without modifying existing classes. Which SOLID principle guides the minimum change, and what is the correct approach?medium
Solution
Step 1: Identify the SOLID principle for extensibility
Open/Closed Principle (OCP) requires adding new functionality without modifying existing code.Step 2: Evaluate approaches
Creating a new FeeStrategy implementation and injecting it respects OCP and DIP; modifying ParkingLot or existing strategies violates OCP or mixes responsibilities.Final Answer:
Option B -> Option BQuick Check:
New FeeStrategy class + injection -> OCP and DIP respected [check]
Hint: New strategy class + injection -> Open/Closed Principle [check]
Common Mistakes:
