OOP design question from Microsoft interview - Kotlin implementation


I came across a post on Leetcode forum about OOP design and the user who posted claimed that he was asked that question in his / her interview.

After seeing the question, I was curious to see if I could implement such design myself, so I tried to attempt it myself. 
 
Question statement: 
 
Problem: Object Oriented Design
 
Design a coffee maker machine class There is a coffee maker with a screen. We need to add three ingredients into the machine: coffee beans, water and milk. There are three types of drinks we can make, below are the default recipes: 
 
Espresso: cost 3 coffee beans and 1 water 
Americano: cost 2 coffee beans and 3 water 
Latte: cost 2 coffee beans, 2 milk and 2 water 
 
When a user comes, on the screen we show available drinks. After the user chooses a drink, the user will be able to customize the amount of ingredients. (For example, after choosing Espresso, the user can change from default to 4 coffee beans and 1 water) 
 
The admin is able to refill the ingredients. The admin and the users interact with the machine via the screen. In the future, we might can support more drink types. 
 
Please design a class with public APIs to represent the coffee maker, which will be called by the screen.


Here's the implementation of the above problem that I came up with based on my understanding of the question. In an interview, I am sure we could clarify and ask questions and implementations may vary.

Here I tried to include the following:

  1. Admin will be able to change and add data of any product, i.e. coffee, soft drinks or could be anything.
  2. Screen class will be notified by Admin when Admin changes or adds any data and Screen class's responsibility is to only show and present said data. 
  3. CoffeeMaker or SoftDrinksMaker are like API which will enable us to add different types of drink. SoftDrinksMaker is an extra API added to demonstrate that this API can be easily extendable and satisfies the condition that in future, Machine can support different types of drinks.
  4. Admin will be able to take input from User and add as many drinks as users want and it will update Screen automatically as I also implemented listener pattern.

Here's the implementation code in Kotlin: 

https://raw.githubusercontent.com/apprajapati9/stackoverflow-mvp/main/app/src/main/java/com/apprajapati/mvp_stackoverflow/kotlin_playground/OOPDesign-Microsoft.kt 

 In Main method, I added necessary code to test all functionality provided by API and different classes. In my opinion, this can be improved further; however, I could do this without any help, so I am content with that. In my opinion, this problem is designed to test your ability to understand abstraction and polymorphism concepts which are essential in an OOP design.

If you have any suggestions to improve or can find any mistakes in the implementation of OOP principles then please comment. Also feel free to ask questions if you want to learn, I am happy to explain.

Post a Comment

0 Comments