Amazon Interview Question
SDE-2sCountry: United States
Interview Type: In-Person
Designing should be in such a way the client code should not be changed at all, for viewing items, selling items, adding inventory to the store. etc To work out these stuff:-
Define a interface as Inventory.
Start defining the composite object like Furniture Item, Electronics Item etc.
Under these add the Concrete Implementation like chair, table, telephone.
Just think in term of composition under each category,
To add the property at the run time isChildSafe, IsWaterSafe use decorator design pattern.
To iterate to all of the inventory, to sell , view use iterator design pattern.
When doing the iteration, different kind of objects are there use visitor design pattern to execute different algorithms on it.
We can have two main classes Product and Inventory.
A product can be any type of product like furniture, cosmetics, phone etc..
It can contain all features like isChildSafe, isFragile and so on.
Inventory will be actual physical items kept in the shop. Every inventory will linked to a product. It can have features like availability,area of storage and so on. Product to Inventory will be one to many relationship
I will use Inheritance to design furniture shop-
- azambhrgn May 11, 2017I will create several classes like-
1- Items - Super class of all classes and will have common variable and methods for all items. such as - id,name,price,getprice(),updatePrice,quantity etc. Because these variables and methods will be common for every item.
2- Furniture- This will be the child class of Item class and can also have several child classes, such as-
a- WoodenFurniture
b- SteelFurniture etc.
Furniture class can also have the boolean flag isChildSafe , Because all the furnitures should have inherit the property of furniture class.
Lets come to the future item addition- Lets say Phone, Tablet , Medicines etc.
You can add all these things by creating several new classes and without making any changes in existing system-
3- Electronics - Child class of item and parent class for the Mobile class,Tablet class and other electronics. You can put boolean like isFireSafe ,isWaterSafe etc. in this class Which are applicable for all the child of Electronics class.
4- Medicine - Again child class of Items, and will have some child class if needed.
You can make Items,Furniture,Electronics,Medicine as abstract classes or Interface(Depends on the requirement).
By using this type of design you can expand your system without affecting existing design.