ISP in Interviews: Common Questions and Machine Coding
Interviewers use ISP questions to assess your ability to identify client needs, refactor bloated designs, and justify design decisions with trade-off analysis.
Common Interview Questions
Question 1: Identify ISP Violation
Given a Printer interface with print(), scan(), fax(), and staple(), identify the violation and refactor.
Answer approach: Note that a simple printer cannot scan, fax, or staple. Segregate into Printable, Scannable, Faxable, and Stapleable. A SimplePrinter implements only Printable. A MultiFunctionPrinter implements all four. Explain that clients like DocumentPrintJob depend only on Printable, avoiding coupling to irrelevant capabilities.
Question 2: ISP vs SRP Confusion
Explain the difference between ISP and SRP (Single Responsibility Principle).
Answer approach: SRP is about classes having one reason to change. A Customer class should not handle both customer data and email sending; split into Customer and EmailService. ISP is about interfaces serving one client role. A Worker interface should not force all implementers to handle eat(); split into Workable and Feedable. SRP focuses on class cohesion. ISP focuses on interface cohesion from the client perspective.
Question 3: When Not to Apply ISP
You have a Repository interface with save(), findById(), and delete(). Should you split it?
Answer approach: Probably not. These three operations represent the cohesive set of CRUD (Create, Read, Update, Delete) behaviors. Most clients performing database operations need all three. Splitting into Saveable, Findable, and Deletable adds complexity without decoupling benefit. However, if you have a read-only reporting client that never saves or deletes, consider ReadOnlyRepository as a separate interface for that specific client. The key is identifying distinct client needs, not mechanically splitting every interface.
Machine Coding Considerations
Parking Lot System: Define Parkable for basic parking operations used by ParkingLotManager. Define ElectricChargeable for charging operations used by ChargingStationController. Define ParkingFeeCalculable for fee operations used by PaymentProcessor. Each system component depends only on its relevant interface slice.
Library Management: Split LibraryItem into Borrowable (for books and DVDs) and Reservable (for items that can be reserved but not yet borrowed). A ReferenceBook is neither borrowable nor reservable. A RegularBook implements both. The CheckoutService depends on Borrowable, while ReservationService depends on Reservable.
Elevator System: An Elevator interface might have move(), openDoor(), closeDoor(), emergencyStop(), and playMusic(). Split into Movable, Doorable, and EmergencyControllable. Background music is a separate concern; if present, define AudioPlayable. A basic elevator implements Movable, Doorable, and EmergencyControllable. A premium elevator adds AudioPlayable.
Interview Red Flags to Avoid
First, do not over-segment. Creating 20 single-method interfaces for a domain with three clients is engineering theater. Interviewers watch for judgment.
Second, do not ignore client identification. If you refactor an interface without explaining which clients need which methods, you miss the point of ISP.
Third, do not conflate ISP with other principles. Saying ISP is the same as SRP or OCP (Open/Closed Principle) shows shallow understanding. Know the distinctions.
Fourth, do not forget exception handling strategy. If you split an interface, explain what happens when a client mistakenly calls a method on the wrong interface type. Does the compiler catch it (type safety), or do you need runtime checks?
Code Smells Indicating ISP Violation
Empty implementations: Methods that do nothing or throw UnsupportedOperationException indicate the implementer does not need that method. The interface is too broad.
Conditional logic in interface methods: If a method body checks object type before acting (if this is ElectricCar then charge, else throw exception), the interface likely bundles unrelated behaviors. Segregate into ElectricChargeable and GasRefuelable.
Comments like not applicable or ignore: If you see comments such as this method is not applicable for this class, the interface violates ISP. Segregate so the class implements only applicable interfaces.
Client casting: If clients cast an interface to a subtype to access additional methods, the interface is too narrow or poorly segregated. Re-examine the interface hierarchy to properly reflect client needs without casting.