Practice
1. In designing the Airline Reservation System, should the
Seat entity be modeled as an abstract class or an interface, and why?easy
Solution
Step 1: Analyze seat characteristics
Seats have shared state (seat number, class) and common behavior (availability, booking), which suggests code reuse.Step 2: Abstract class vs interface
Abstract class allows shared state and default behavior implementation, while interface cannot hold state and only defines method signatures.Final Answer:
Option A -> Option AQuick Check:
Shared state + behavior -> abstract class [check]
Hint: Shared state needs abstract class, interfaces lack fields [check]
Common Mistakes:
2. In designing a
Vehicle abstraction for a parking system, should Vehicle be an abstract class or an interface? Consider that vehicles have state like license plate and behavior like park().easy
Solution
Step 1: Analyze state and behavior requirements
Vehicles have shared state (e.g., license plate) and common behavior (e.g., park). Abstract classes can hold state and provide default method implementations.Step 2: Eliminate interface options
Interfaces cannot hold state (prior to Java 8 default methods) and rely on external storage for state, which complicates design. Abstract class better encapsulates state and behavior.Final Answer:
Option A -> Option AQuick Check:
Shared state + default behavior -> abstract class [check]
Hint: Stateful base class -> abstract class, not interface [check]
Common Mistakes:
3. The Meeting Scheduler currently calculates fees using a
FeeCalculator class. To add a new WeekendFeeStrategy without modifying existing code, which SOLID principle applies and what is the minimum change required?medium
Solution
Step 1: Identify SOLID principle for extensibility
Open/Closed Principle (OCP) requires adding new behavior without modifying existing classes.Step 2: Apply Dependency Inversion Principle (DIP)
Injecting the new WeekendFeeStrategy via constructor or setter allows FeeCalculator to depend on abstractions, enabling extension without modification.Final Answer:
Option A -> Option AQuick Check:
Inject new strategy -> respects OCP and DIP [check]
Hint: Inject new strategy -> OCP + DIP respected [check]
Common Mistakes:
4. Consider a
tryConsume() method in TokenBucketRateLimiter that checks if tokens are available and then decrements the count without synchronization. Which exact thread interleaving causes double token consumption?medium
Solution
Step 1: Identify check-then-act race condition
Without synchronization, both threads can read tokens > 0 before either decrements.Step 2: Trace exact interleaving causing double consumption
Thread 1 checks tokens > 0 -> true, context switch; Thread 2 checks tokens > 0 -> true; both proceed to decrement, consuming same token twice.Final Answer:
Option D -> Option DQuick Check:
Unsynchronized check-then-act allows double token consumption [check]
Hint: Check tokens > 0 twice before decrement -> double consume [check]
Common Mistakes:
5. You need to add support for
ElectricCar and ChargingSpot in the parking system without modifying existing classes. What is the minimum change to the current design?hard
Solution
Step 1: Identify extensibility without modifying existing classes
Enums can be extended with new constants, and new subclasses can be added without changing existing code.Step 2: Use external compatibility map update
Updating the compatibility map externally avoids modifying existing classes, preserving OCP and avoiding class explosion.Step 3: Avoid flags or interface checks that require existing class changes
Flags or interface checks require modifying existing classes, violating OCP.Final Answer:
Option B -> Option BQuick Check:
Extend enums + new subclass + external map update -> minimal change [check]
Hint: Extend enums + subclass + external map -> minimal change [check]
Common Mistakes:
