Amazon Interview Question for Software Engineer / Developers


Country: United States




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

C++: Base Shape class

class shape {
	public:
		shape();
		virtual double area() const;
		virtual double perimeter() const;
	private:
		coordinates position;
		color outline, fill;
};

C Equivalent:

typedef struct shape shape;
typedef struct shape_vtbl shape_vtbl;

struct shape_vtbl {
	double (*area)(shape const *s);
	double (*perimeter)(shape const *s);
};
struct shape {
	shape_vtbl *vptr;
	coordinates position;
	color outline, fill;
};

C++: circle class derived from shape looks like:

class circle: public shape {
	public:
		circle(double r); 
		virtual double area() const;
		virtual double perimeter() const;
	private:
		double radius;
};

#include "shape.h"
typedef struct circle circle;
struct circle {
	shape base; // the base class sub-object
	double radius;
};
void circle_construct(circle *c, double r);

In C++, a call to the virtual area function applied to a shape looks exactly like a non-virtual call, as in:

shape *s;
~~~
s->area();

In C, virtual function calls look unlike any other kind of function call. For example, a call to the virtual area function applied to a shape looks like:

shape *s;
~~~
s->vptr->area(s);

In this case, if s points to a circle (the dynamic type of *s is circle), then the call above calls circle_area. If s points to a rectangle, then the call above calls rectangle_area.

Source: forum.eetindia.co.in/BLOG_ARTICLE_13544.HTM

- R@M3$H.N September 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Just use a table of function pointers.

- ravio September 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In C++, a virtual function can be redefined in a derived class

class Shape {
public:
virtual int area (void) =0;
};

class Square: public Shape {
public:
int width;
int area (void)
{ return (width * width); }
};

- Anonymous September 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

xyz

- Anonymous September 25, 2014 | Flag Reply


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