Amazon Interview Question
Software Engineer / DevelopersCountry: India
could somebody please explain the question in a bit more detailed way.... wat actually we have to do....
this is not something to close, since we don't have proper info.. assume the best.. and give the design that can do stuff.
Optimizing the code has to be written in the cloak room, which calls add, remove items and optimizes the space.
public class Item{
static int count = 0;
public Item(String name,int storage){
this.name = name;
this.storage = storage;
this.id = count;
count++;
}
string name;
int id;
int storage;
int getStorage(){
return storage;
}
int getId(){}
}
public class Compartment{
public Compartment(int s){
capacity = s;
}
int capacity;
int filled = 0;
List<Items> items;
bool isSpaceAvailable(){
}
bool addItem(Item item){
return true; // if space available
else return false;
}
Item deleteItem(int id){
}
}
public class CloakRoom{
Compartment one(1),two(2),three(3);
// in order to implement small, medium, large -- enum can be used.
}
An interface Compartment, implemented by 3 types of compartment (Small, Medium, Large)
A class CloakRoom, which contains a reference of Compartment.
A System containing a collection of CloakRoom objects.
Not sure about how optimization can be brought into picture. Can someone help on this?
4S 2M 1L
- SantoshRameshraoDeshmukh February 01, 2012#define uint Token
struct item
{
char data/maal[];
};
class Compartment
{
int space = 10;
int count = 0;//initially no items / empty compartment
public :
Compartment()
{
space = 10;
count = 0;
}
/*bool open()
{return true;}
void close()
{}*/
void push_item(Item i1)
{
if( count < space )
{
item[count++] = i1;
}
}
Item pop_item()
{
if( count > 0 )
{
count--;
return item[count];
}
}
bool isSpaceAvailable()
{
return( count < space )
}
Item item[10+1];//Assuming one compartment can hold 10 items
};
class SmallCompartment : public Compartment
{
};
class MediumCompartment : public Compartment
{
SmallCompartment c1, c2;
public :
bool isSpaceAvailable()
{
return c1.isSpaceAvailable() || c2.isSpaceAvailable()
}
};
LargeCompartment : public Compartment
{
MediumCompartment m1, m2;
public :
bool isSpaceAvailable()
{
return m1.isSpaceAvailable() || m2.isSpaceAvailable()
}
};
CloakRoom
{
uint itemCount = 0;//This tells whether there are items available in cloak room or not
//Assume there are few Small, Medium, Large size compartments in a cloak room
uint SmallCompartmentCount = 5;
uint MediumCompartmentCount = 3;
uint LargeCompartmentCount = 2;
//Assume there are few Small, Medium, Large size compartments in a cloak room
TArray<SmallCompartment, SmallCompartmentCount> SmallCompartments;
TArray<MediumCompartment, MediumCompartmentCount> MediumCompartments;
TArray<LargeCompartment, LargeCompartmentCount> LargeCompartments;
public:
Token# put_item(Item i)
{
if( SmallCompartments.isSpaceAvailable() )
{
itemCount++;
return SmallCompartments.Add( i );//Assuming we are returning TArray index here as Token#
}
else if( MediumCompartments.isSpaceAvailable() )
{
itemCount++;
return MediumCompartments.Add( i );
}
else if( LargeCompartments.isSpaceAvailable() )
{
itemCount++;
return LargeCompartments.Add( i );
}
return -1;
}
Item get_item(Token tokenNo)
{
if( SmallCompartments.find(tokenNo) )
{
itemCount--;
return SmallCompartments.Remove[tokenNo];
}
else if( MediumCompartments.find(tokenNo) )
{
itemCount--;
return MediumCompartments.Remove[tokenNo];
}
else if( LargeCompartments.find(tokenNo) )
{
itemCount--;
return LargeCompartments.Remove[tokenNo];
}
return NULL;
}
bool isSpaceAvailable()
{
return SmallCompartments.isSpaceAvailable() || MediumCompartments.isSpaceAvailable() || LargeCompartments.isSpaceAvailable();
}
//This indicates that there is some item available in cloak room
bool isItemAvailable()
{
return (itemCount > 0);
}
};
void main()
{
CloakRoom cr;
if( cr.isSpaceAvailable() )
{
cout << "How may I help you." << endl;
Item item1;
item1.data = "Jalebi";
Token tok = cr.put_item(item1);//Add item to cloak room
}
if ( cr.isItemAvailable )
{
Item item = cr.get_item( tok );//Take item from cloak room
}
}//end main