Practice
1. In designing the Amazon Locker Service, should the
Package entity be modeled as an abstract class or an interface, and why?easy
Solution
Step 1: Analyze Package state and behavior
Package has shared mutable state such as OTP, expiry time, and tracking info, which requires fields and common method implementations.Step 2: Abstract class vs interface suitability
Abstract classes can hold state and provide default behavior, interfaces cannot hold mutable state (prior to Java 8 default methods, but still no fields). Therefore, abstract class fits better.Final Answer:
Option B -> Option BQuick Check:
Shared mutable state -> abstract class needed [check]
Hint: Stateful entities -> abstract class; stateless behaviors -> interface [check]
Common Mistakes:
2. In designing a collection class that supports iteration, should the iterator interface be an abstract class or an interface in Java, and why?
easy
Solution
Step 1: Understand Java interface vs abstract class
Interfaces define behavior contracts without state, allowing multiple implementations. Abstract classes can hold state but restrict multiple inheritance.Step 2: Apply to iterator design
Iterator interface defines traversal methods (e.g.,hasNext(),next()) without state, enabling different iterator implementations per collection type.Final Answer:
Option D -> Option DQuick Check:
Iterator as interface enables flexible, stateless traversal contracts [check]
Hint: Iterator interface defines behavior, no state needed [check]
Common Mistakes:
3. To add a
WeekendFeeStrategy for pricing in the current product catalog design, which change respects SOLID principles with minimum modification?medium
Solution
Step 1: Identify applicable SOLID principle
The Open-Closed Principle (OCP) requires that classes be open for extension but closed for modification.Step 2: Why creating a new class is best
Implementing a newWeekendFeeStrategyclass extends functionality without modifying existing code, preserving OCP and avoiding side effects.Final Answer:
Option A -> Option AQuick Check:
New strategy class -> OCP compliance, no existing class changes [check]
Hint: OCP: extend with new class, don't modify existing [check]
Common Mistakes:
4. The current parking lot design 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: Apply OCP by adding new strategy class
ImplementWeekendFeeStrategyas a new class implementingFeeStrategyinterface, no changes to existing classes needed.Final Answer:
Option A -> Option AQuick Check:
New class implements interface -> existing code unchanged -> OCP [check]
Hint: Add new strategy class, no existing code change -> OCP [check]
Common Mistakes:
5. The Restaurant Management System needs to support ElectricCar vehicles that require ChargingSpots. What is the minimum change to the current Java design to add this feature without modifying existing classes?
hard
Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and subclasses without changing existing classes respects OCP and avoids class explosion.Step 2: Update compatibility externally
Updating SpotCompatibility map outside existing classes allows new vehicle and spot types to integrate without modifying existing code.Final Answer:
Option A -> Option AQuick Check:
New enums + subclass + external compatibility update -> minimal change [check]
Hint: Add enums + subclass + external map update -> minimal change [check]
Common Mistakes:
