Interview Deep Dive: Polymorphism in Machine Coding
Common Interview Questions
Question 1: "Design a parking lot system. How would you use polymorphism?"
Second, Define abstraction: Create
Vehicle base class with calculateParkingFee(duration) and getRequiredSpotSize() methods.Third, Show extensibility:
ParkingLot.parkVehicle(vehicle) works with any vehicle type. Adding Bus requires only creating the new class.Fourth, Address exceptions: If fee calculation becomes complex (dynamic pricing based on time of day), consider Strategy Pattern for pricing instead of embedding logic in each vehicle class.
Question 2: "How does polymorphism help you follow SOLID principles?"
ParkingLot class is closed for modification but open for extension via new Vehicle subclasses.Liskov Substitution Principle (LSP): Subtypes must be substitutable for base types. If
parkVehicle expects a Vehicle, passing a Car or Truck must work correctly without breaking assumptions.Dependency Inversion Principle (DIP): High-level modules depend on abstractions (Vehicle), not concrete implementations (Car). This decouples the parking logic from specific vehicle types.
Machine Coding Considerations
Consideration 1: Interface vs Abstract Class. Use an interface when types do not share implementation (e.g., PaymentMethod). Use an abstract class when types share common attributes or helper methods (e.g., Vehicle with registrationNumber field). If unsure, start with an interface; refactor to abstract class if duplication emerges.
Consideration 2: Method Granularity. Make polymorphic methods focused. Instead of one large process() method, use specific methods like validate(), execute(), calculateFee(). This makes implementations clearer and supports Single Responsibility Principle (SRP).
Consideration 3: Avoid Type Checking. If you find yourself using instanceof or type checks in client code, you have violated polymorphism. The entire point is to eliminate such checks. If needed, consider Visitor Pattern or double dispatch for complex type-dependent operations.
fee = hours * 10
else if vehicle instanceof Motorcycle:
fee = hours * 5
else if vehicle instanceof Truck:
fee = hours * 20
calculateFee() method.Interview Variations
Variation 1: "The client wants to add dynamic pricing (weekday vs weekend rates). How do you modify your design?"
Answer: Do not modify the Vehicle hierarchy. Instead, introduce a PricingStrategy interface with WeekdayPricing and WeekendPricing implementations. Compose this into the ParkingLot or pass it to calculateFee(duration, pricingStrategy). This combines polymorphism (via Strategy) with composition, keeping the vehicle hierarchy stable.
Variation 2: "What if vehicles can have multiple roles (electric and luxury)? How do you avoid combinatorial explosion?"
Answer: Avoid creating ElectricLuxuryCar, ElectricStandardCar, etc. Use composition with interfaces: ElectricPowered and LuxuryAmenities as separate interfaces. A Car can implement both. Alternatively, use Decorator Pattern to add features dynamically.