Amazon Interview Question for SDE1s


Country: United States
Interview Type: Phone Interview




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

Create an abstract "shape" class with an abstract method called "getArea". Create concrete classes for each specific shape that implements the "getArea" method (you could use the factory pattern to handle the creation of these object types). Because all these objects extend "shape" you could you store them all in the same collection type. For example, in Java, Collection<Shape> shapes;

- Anonymous August 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your solution is missing the second requirement. In java your abstract shape class would also have to implement the comparable interface that would compare 2 shapes using the getArea() function so the collection array can use its sorting function

- houseUrMusic August 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

HelpfulWhere would we define the method for sorting all the shapes?

- Srikanth September 26, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Srikanth once you implement comparable interface on the object say "Shape" the collection.sort method will take care of sorting, you don't need to explicitly write it.

- siva September 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Create on factory class which will create shape objects based ont he parameters passed by client.
Create one class which will calculate area on the basis of shape object passed.
Can't think of more than this.

- OTR August 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

1)create interface Area with method getArea(double ... parameters)
2)have concrete implemetations like rectArea, circArea etc which have getArea method implemented accordingly.
3) Have a base class Shape implements Comparable and which has Area as a member field.
4) Have a constructor like Shape(Area requiredArea){ this.Area = requiredArea) and a method getArea(){ return Area.getArea())
5) Have comparable to act on Area.getArea() of Shape class.
6) Since all are type of Shape class, hence they can be stored in collection of type Shape.

- m3th0d.itbhu August 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Using Comparable interface :

public class Shape implements Comparable<Shape>{

...

..

@Override
public int compareTo(Shape shape) {
return this.getArea() - shape.getArea();
}
}

Using Comparator :

somewhere in code :

Collections.sort(shapes, new Comparator<Shape> {
@Override
public int compare(Shape s1, Shape s2) {
return s1.getArea() - s2.getArea();
}
})

- nir October 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class shape {
public:
double area;
public:

virtual double getArea(){}

};
class circal:public shape
{
double radius;
public:
double getArea(){return area;}
circal(int r):radius(r){area = pi*radius*radius ;}
};
class rec:public shape
{
double eage;
public:
double getArea(){ return eage*eage;}
rec(int e):eage(e){area = eage*eage;}
};
bool cmp(shape *p1,shape *p2)
{
return p1->area<p2->area;
}
vector<shape *> v;

- ICT_100 October 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public abstract class Shape implements Comparable<Shape>{

protected Integer area ;
public abstract Integer calculateArea();

@Override
public int compareTo(Shape shape) {

if(null == shape)
return 0;
return (this.calculateArea() - shape.calculateArea());
}

}




========================================
package design.shapeexample;

public class Recatangle extends Shape{

private Integer length;
private Integer width;

public Recatangle() {
// TODO Auto-generated constructor stub
}

public Recatangle(Integer length, Integer width){
this.length = length;
this.width = width;

}

@Override
public Integer calculateArea() {
this.area = this.length * this.width;
return this.area;
}

@Override
public String toString() {
// TODO Auto-generated method stub
return area.toString();
}
}

=========================================

package design.shapeexample;

public class Square extends Shape{

private Integer dimension;

public Square() {

}

public Square(Integer dimension){

this.dimension = dimension;
}


@Override
public Integer calculateArea() {
area = this.dimension * this.dimension;
return area;
}

@Override
public String toString() {
// TODO Auto-generated method stub
return area.toString();
}
}

- Anonymous May 08, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.


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