Practice
1. In designing a
Command interface in Java, should it be an abstract class or an interface, and why? Consider that commands may need to store state for undo functionality.easy
Solution
Step 1: Analyze state requirements for undo
Commands often need to store state (e.g., previous values) to support undo, which requires fields.Step 2: Compare interface vs abstract class in Java
Interfaces cannot hold instance fields, so abstract classes are better to share common state and default undo logic.Final Answer:
Option A -> Option AQuick Check:
Abstract class supports shared state and behavior for undo [check]
Hint: Use abstract class for shared state in commands [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. In a multi-threaded parking system using the Decorator pattern, the
parkVehicle() method checks if a spot is available and then marks it occupied. Without synchronization, which exact thread interleaving causes double-booking?medium
Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads spot as available (true) but before marking it occupied, context switches.Step 2: Trace Thread 2's actions
Thread 2 also reads spot as available (true) and proceeds to mark it occupied. Both threads think the spot is free and park vehicles, causing double-booking.Final Answer:
Option A -> Option AQuick Check:
Check-then-act without sync allows both threads to park simultaneously [check]
Hint: Check-then-act gap causes double-booking without sync [check]
Common Mistakes:
4. 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 the SOLID principle involved
The Open/Closed Principle (OCP) requires classes to be open for extension but closed for modification.Step 2: Apply OCP to fee strategy
Creating a newWeekendFeeStrategyclass extends behavior without modifying existing code, adhering to OCP and Dependency Inversion Principle (DIP).Final Answer:
Option C -> Option CQuick Check:
New strategy class -> extend without modifying existing code [check]
Hint: OCP: add new class, don't modify existing ones [check]
Common Mistakes:
5. The Elevator System needs to support
ElectricCar vehicles requiring ChargingSpot types. What is the minimum change to the current Java design to add this without modifying existing classes?hard
Solution
Step 1: Extend enums and create subclass
Adding new enum constants for vehicle and spot types allows type-safe extension without changing existing classes.Step 2: Update compatibility map externally
Updating SpotCompatibility.COMPATIBILITY_MAP to include ElectricCar and ChargingSpot relations extends behavior without modifying existing class code.Final Answer:
Option A -> Option AQuick Check:
Enum extension plus compatibility map update avoids modifying existing classes [check]
Hint: Extend enums and maps, no existing class changes [check]
Common Mistakes:
