Amazon Interview Question
Software Engineer / DevelopersThere are two flaws:
1. The no.of parts ie you never keep a count of parts ie the Arm class shall not be instantiated more than twice,we can't have 3 hands and 4 legs and 2 heads.You have to keep a check.
2.You forgot the most important body part. Haven't you heard that AD's were put in use before helmets in Cricket.
Question says Body and not BodyPart,
this design is good I agree,
but not in the limits of the question conditions
//I think it's a case of composition.All the classes are related to eachother in a form of HAS-A relationship. Arm,Leg,Head are part of Body.If body ceases to exist,they will also cease to exist. Same with Person.Person IS NOT a body,Person has a body and furthermore, body object will come its its end if Person objects ceases to exist.
class Person
{
protected Body _body;
public void HasBody()
{
this._body = new Body();
}
}
public class Body
{
//Head,Arm,Legs are part of the Body Class
public Head _head;
protected Arm _arm;
protected Leg _leg;
public void SetHead()
{
this._head = new Head();
}
public void SetArm()
{
this._arm = new Arm();
}
public void SetLeg()
{
this._leg = new Leg ();
}
}
public class Head
{
//Whatever code
}
public class Arm
{
//Whatever Code
}
public class Leg
{
//Whatever Code
}
These can be related by polymorphism. Person becomes the parent class. Body, Head, Arm, Leg all inherit Person as a person contains all of the 4. So by inheritance, Body, Head, Arm, Leg became of "Person Type". Any pointer or reference of Person Type can now point to these four class objects too. Using Virtual functions we can call object specific member functions which override the parent member. Eg.
#include<iostream>
using namespace std;
class Person {
public:
virtual void id(){ cout << "Person::id()" << endl;};
};
class Body: public Person{
public:
void id(){ cout << "Body::id()" << endl;};
};
class Arm: public Person{
public:
void id(){ cout << "Arm::id()" << endl;};
};
class Head: public Person{
public:
void id(){ cout << "Head::id()" << endl;};
};
class Leg: public Person{
public:
void id(){ cout << "Leg::id()" << endl;};
};
void body_part(Person& P)
{ cout << "\nBody Part is :";
P.id();
}
int main()
{ Arm A; Leg L;
body_part(A);
body_part(L);
getchar();
return 0;
}
Output: Body part is:Arm::id()
Body part is:Arm::id()
Output: Body part is:Arm::id()
Body part is:Leg::id()
Don't if this is the relationship the interviewer wanted. Correct me if I am wrong.
An arm, leg, or head is not a type of person. The are parts of a body and a person has one (or zero I suppose) body.
I would do it this way:
abstract class BodyPart
class Body 1<>----1..* BodyPart
class Head : public BodyPart
class Arm : public BodyPart
class Leg : public BodyPart
class Person
# m_body : Body
Actually thinking about it, you don't need Arm, Head, Leg to be different classes. You can distinguish them when you instantiate them. So BodyPart isn't abstract.
devesh, i would disagree. inheritance does not apply here. inheritance models a "is-a" relationship. arm, leg, head and body do not have a "is-a" relation with person. composition fits better. we can probably incorporate the fact that a person must definitely have a head and body and introduce multiplicity for arms and legs. But composition is the way to go.
// How about this
- David June 23, 2010class Person
{
string FirstName;
string LastName;
Body bd ();
}
abstract class BodyPart
{
int length;
int getLength ()
{
return length;
}
}
class Arm extends BodyPart {}
class Hand extends BodyPart {}
class Leg extends BodyPart {}
class Head extends BodyPart {}
class Body
{
Arm left();
Arm right();
Leg left();
Leg right();
Head head();
}