Practice
1. In designing the ATM system, should the
Card entity be modeled as an abstract class or an interface, considering it has state (card number, expiry) and behavior (validate PIN)? Why?easy
Solution
Step 1: Analyze Card's characteristics
Card holds state (card number, expiry) and behavior (validate PIN), so it needs to share state and common logic.Step 2: Choose between abstract class and interface
Abstract class supports shared state and behavior inheritance. Interface cannot hold instance state, only constants and default methods without instance fields.Final Answer:
Option A -> Option AQuick Check:
Shared state + behavior -> abstract class [check]
Hint: Stateful base entity -> abstract class, not interface [check]
Common Mistakes:
2. The current Tic-Tac-Toe design uses a
WinStrategy interface for win detection. To add a WeekendWinBonusStrategy that applies only on weekends, what is the minimum change required to comply with SOLID principles?medium
Solution
Step 1: Understand OCP and DIP in current design
Open-Closed Principle (OCP) requires adding new behavior without modifying existing classes. Dependency Inversion Principle (DIP) encourages depending on abstractions.Step 2: Evaluate options for minimal change
Create a newWeekendWinBonusStrategyclass implementingWinStrategyand use it without modifying existing classes. adds a new strategy class but requires changing client code to use it, violating minimal change. Add a boolean flag in theBoardclass to toggle weekend mode and update all win checks accordingly. adds a toggle flag in Board, allowing existing strategies to adapt behavior without modifying their code, preserving OCP better than A or D.Final Answer:
Option B -> Option BQuick Check:
Toggle flag in Board -> minimal change, respects OCP [check]
Hint: OCP: add new class or toggle flag, avoid modifying existing [check]
Common Mistakes:
3. 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:
4. You need to add support for
ElectricCar and ChargingSpot in the existing parking system using the Iterator pattern. What is the minimum change to the current design to support this without modifying existing classes?hard
Solution
Step 1: Identify extensibility requirements
Adding ElectricCar and ChargingSpot should not require modifying existing classes to respect OCP.Step 2: Evaluate options
Add ELECTRIC_CAR to VehicleType enum, CHARGING to SpotType enum, create ElectricCar subclass, and update SpotCompatibility map; existing classes remain unchanged. adds new enums and classes without modifying existing ones, preserving OCP. Modify Vehicle and Spot classes to include electric-specific fields and logic, then update iterators accordingly. modifies existing classes, violating OCP. Create a new ElectricVehicle interface extending Vehicle and a ChargingSpot class, then modify existing iterators to handle them. adds interfaces but requires modifying iterators, violating OCP. Add a flag isElectric to Vehicle class and modify SpotCompatibility to check this flag during iteration. modifies Vehicle class, violating OCP.Final Answer:
Option A -> Option AQuick Check:
Adding new enums and subclasses without changing existing classes respects OCP [check]
Hint: Add new types via enums and subclasses, no existing class changes [check]
Common Mistakes:
5. ElectricCar requires ChargingSpots in the parking lot system. What is the minimum change to support this without modifying existing classes?
hard
Solution
Step 1: Identify extensibility requirements
Supporting ElectricCar and ChargingSpots requires new types without changing existing class logic to respect OCP.Step 2: Apply minimal change approach
Adding new enum values and subclass ElectricCar allows extension without modifying existing classes. Updating SpotCompatibility map centralizes compatibility logic.Step 3: Eliminate options violating OCP
Options B, C, and D modify existing classes or add conditional logic, violating OCP and risking class explosion or god classes.Final Answer:
Option C -> Option CQuick Check:
New enums + subclass + compatibility map update -> minimal, OCP-compliant [check]
Hint: Add enums + subclass + compatibility map, no existing class changes [check]
Common Mistakes:
