OOP FundamentalsAbstraction & InterfacesHard⏱️ ~3 min

Interview Scenarios: Abstraction in Machine Coding Rounds

Machine coding rounds test your ability to apply abstraction pragmatically within tight time constraints. Here are common scenarios and how to handle them.

Scenario 1: "Design a Library Management System"
Key Abstraction Decision: Should Book, Magazine, and DVD implement a LibraryItem interface or extend an abstract LibraryItem class?
Answer: Use abstract class. All items share state (itemId, title, checkedOut status) and behavior (checkOut, returnItem). Specific items override calculateLateFee() because fees differ. This is IS-A relationship with shared implementation.
«abstract» LibraryItem
# itemId: String
# title: String
# isCheckedOut: Boolean
+ checkOut(): Boolean
+ returnItem(): void
+ calculateLateFee(days): Money
Book
+ calculateLateFee()
Magazine
+ calculateLateFee()
DVD
+ calculateLateFee()
Scenario 2: "Add Search Functionality to Library"
Key Abstraction Decision: Should search strategies (by title, by author, by ISBN) use a SearchStrategy interface?
Answer: Yes, if interviewer mentions "multiple search types" or "extensible search." The Strategy Pattern with SearchStrategy interface allows Library class to delegate to TitleSearchStrategy, AuthorSearchStrategy, etc. However, if time is tight and only one search type is requested, implement it directly and mention "this can be abstracted later if we need multiple strategies." Pragmatism over perfection in timed settings.
Scenario 3: "Design an Elevator System"
Key Abstraction Decision: Should the scheduling algorithm (FCFS, SCAN, LOOK) be abstracted?
Answer: Absolutely. Scheduling algorithms are the core complexity and interviewers often ask "what if we change the algorithm?" Use an ElevatorScheduler interface with implementations like FCFSScheduler and SCANScheduler. The ElevatorController depends on the interface, not concrete schedulers. This demonstrates understanding of OCP (Open/Closed Principle).

Common Interview Questions:

Q: "Why did you use an interface here instead of an abstract class?"
A: Explain the lack of shared state or the need for multiple inheritance. For example: "The Notifiable interface allows both User and Administrator classes to receive notifications without forcing them into a common inheritance hierarchy. Users and Administrators are fundamentally different entities, so interface composition is more appropriate than inheritance."

Q: "How would you add a new payment method without changing existing code?"
A: Demonstrate OCP understanding: "I would create a new class implementing PaymentMethod interface, such as CryptocurrencyPayment. The ParkingTransaction class requires no changes because it depends on the PaymentMethod abstraction, not concrete implementations. I would register the new payment method in the payment factory or dependency injection container."

Q: "Is this abstraction necessary?"
A: Be prepared to justify or simplify. If you created an interface with a single implementation and no clear need for variation, admit it: "You are right, this abstraction is premature. Since we only have one implementation and no anticipated variations, I will refactor to use the concrete class directly." Recognizing over-engineering is a strength, not weakness.

Interview Tip: Time management is critical. In a 60-minute round, spend 10 minutes on design discussion, 40 minutes coding, and 10 minutes on testing/refinement. Use interfaces where justified, but do not over-abstract. Interviewers value working code with sound reasoning over perfect architecture with incomplete implementation.
💡 Key Takeaways
Use abstract classes for library items with shared state and behavior, interfaces for strategies without shared implementation
Justify abstraction decisions explicitly during interviews, admitting over-engineering when appropriate
Focus on core abstractions (scheduling algorithms, payment methods) rather than abstracting everything
Time management: prioritize working code with justified abstractions over perfect but incomplete designs
📌 Examples
1Library Management: LibraryItem abstract class for shared checkout logic, but item-specific late fee calculation
2Elevator System: ElevatorScheduler interface allows swapping FCFS, SCAN, LOOK algorithms without changing controller
← Back to Abstraction & Interfaces Overview