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. You want to add a
WeekendFeeStrategy to calculate extra fees for weekend playback in the current music library design. Which approach requires the minimum change while respecting SOLID principles?medium
Solution
Step 1: Identify SOLID principle involved
Open-Closed Principle (OCP) requires adding new behavior without modifying existing code.Step 2: Evaluate options against OCP
Create a newWeekendFeeStrategyclass implementing the existingFeeStrategyinterface and inject it where needed. adds a new strategy class implementing FeeStrategy, no existing class modified, respecting OCP and Dependency Inversion Principle.Final Answer:
Option B -> Option BQuick Check:
New strategy class -> no existing code change -> OCP respected [check]
Hint: OCP: add new class, don't modify existing [check]
Common Mistakes:
4. You need to add support for
ElectricCar that requires ChargingSpot compatibility in the current parking system. What is the minimum change to the design to support this without modifying existing classes?hard
Solution
Step 1: Identify extensibility requirements
ElectricCar and ChargingSpot are new types requiring compatibility mapping without changing existing classes.Step 2: Apply minimal invasive change
Adding new enum values and subclass plus updating compatibility map extends functionality without modifying existing class code, preserving OCP.Step 3: Avoid violating OCP
Options B, C, and D modify existing classes or create parallel hierarchies, increasing complexity and violating OCP.Final Answer:
Option B -> Option BQuick Check:
Extend enums and map, subclass ElectricCar -> minimal change [check]
Hint: Extend enums and map, subclass new type -> minimal change [check]
Common Mistakes:
5. ElectricCar needs to park in ChargingSpots. What is the minimum change to the current design with
VehicleType and SpotType enums, and SpotCompatibility map, to support this without modifying existing classes?hard
Solution
Step 1: Identify extensibility requirements
Adding ElectricCar and ChargingSpot requires new enum values and a new class without modifying existing classes to respect OCP.Step 2: Apply minimal change with enums and composition
Adding new enum constants and extendingVehiclewithElectricCarallows reuse of existing logic. UpdatingSpotCompatibility.COMPATIBILITY_MAPwith new mappings supports compatibility without changing existing code.Step 3: Eliminate options violating OCP
Options B, C, and D modify existing classes or require widespread changes, violating OCP and risking regressions.Final Answer:
Option B -> Option BQuick Check:
New enums + subclass + compatibility map update -> minimal, OCP-compliant [check]
Hint: Add enums + subclass + map update, no existing class changes [check]
Common Mistakes:
