Amazon Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
3
of 3 vote

// How about this

class 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();
}

- David June 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think this design looks really really nice. You use inheritance, composition ...

- Anonymous January 12, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I agree with this design

- swapnil.joshi4886 March 19, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

agree!!

- chandan.here4u May 21, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

There 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.

- Tim Paine June 22, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Really good one. Thanks

- Ashish February 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Question says Body and not BodyPart,
this design is good I agree,
but not in the limits of the question conditions

- Ashupriya July 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What if one wud like to add more body-parts later, is the above design flexible enough ? Or you require to modify class Body in that case ?

- spiderman August 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

Composition...

- gevorgk June 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

bosy is composed of Arm, Leg, Head.
Every Person has body.

- anil June 08, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No, body can be just the torso without arms and legs

- Anonymous June 08, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No, body can be just the torso without arms and legs

- Anonymous June 08, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

true, but that's still composition, it changes only the cardinality.... body can contain 0 to 2 of hands etc.

- phm June 10, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

i think use
arm,leg,head as base class
inherit into body class
which is inherited into person class..

- gold July 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@David - very nice solution. Thanks

- dhrubo July 31, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

I think.... Person consists of all the other classes with better explanation done.

- Karthik December 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I would add a List<BodyPart> to the Person class, that would keep a single list of bodyparts as well. Otherwise the BodyPart abstraction is not used.

- dantepy April 30, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//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
}

- stergiazotali February 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

A good model is probably not possible with object-oriented modeling.

- Mishra.Anurag June 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

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()

- Devesh June 18, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- Devesh June 18, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Colleen June 22, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- Colleen June 22, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- cavey July 26, 2010 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More