Amazon Interview Question for Web Developers


Country: United States




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

Very much off the top of my head.

Have a GraphicObject (interface or abstract class) that defines common operations and accessor for x,y coordinates. Some GraphicObjects such as paths may be comprised of sub-objects such as vertices (so a path is drawn from vertex to vertext). Also have a selection object that encapsulates several objects as a selection (composite pattern).

Each could also include a draw method that draws itself and its subobjects, or it could have a method which accepts a rendering object (ConcreteRenderer implements Renderer) which figures out how to draw each concrete object. The second approach would be more flexible. For example, you could then have a screen-renderer, a PDF-renderer, a Black-and-white renderer etc.

At the document level, don't expose any of these objects directly. Instead, use a state pattern. Just have a "Change Tool" method which changes the currently active tool, and a mouse-down and mouse-up method which are called when the user presses or releases the mouse on the drawing surface. When "Change Tool" is called, the Document sets the current tool and then subsequent calls to mousedown/up are passed to that tool.

For example, the user may change the tool to the select tool, then press the mouse down, drag, and release. These are passed to the select tool, which calculates which objects are selected and sets the Document's current Selection object to that selection.
Next the user chooses the "Move" tool. Subsequent mouse-downs and ups are interpreted as move requests for the currently active selection. The Move-tool object calls the selection's move method, and the selection calls the move method on all of its children.
Ditto with other commands, such as creation commands.
Also have a mediator which handles interaction between the document and the various GUI elements (such as the tool selection buttons).

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

Sounds quite reasonable, but could you please share some piece of the code, it's easier to read code sometimes

- Cheney March 30, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Vertex{
	private:
		double _x, _y;
	public:
		Vertex(double x, double y){
			_x = x;
			_y = y;
		}

		Vertex(){
			_x = 0;
			_y = 0;
		}

		void setX(double x){
			_x = x;
		}

		void setY(double y){
			_y = y;
		}

		int getX(double x){
			return _x;
		}

		int getY(double x){
			return _y;
		}
};

class Graphics{
	public:
		virtual void draw() = 0;
		virtual void fill() = 0;
		virtual void move() = 0;
		virtual void resize() = 0;
		virtual double getArea() = 0;
};

class Line:public Graphics{
	private:
		Vertex _left;
		Vertex _right;
		double _lineThickness;

	public:
		void draw();
		void fill(){
		}
		void move();
		void resize();
		double getLength();
		double getArea(){
			return 0;
		}
		double getThickness();
};

class Rectangle: public Graphics{
	private:
		Vertex _leftUp;
		Vertex _leftDown;
		Vertex _rightUp;
		Vertex _rightDown;

	public:
		void draw();
		void fill();
		void move();
		void resize();
		double getWidth();
		double getHeight();
		double getArea();
};

class Text: public Graphics{
	private:
		std::string _text;
		Rectangle _rectangleToHoldText;
		int _font;
		bool _isBold;
		bool _isItalic;

	public:
		void draw(); //Basically create a rectangle and write the text in it.
		void fill(){
			//Do Nothing
		}
		void move(); //Move the rectangle
		void resize();
		double getArea(){
			return rectangleToHoldText.getArea();
		}
};

class Circle: public Graphics{
	private:
		Vertex _center;
		double _thickness;
		double _radius;

	public:
		void draw();
		void fill();
		void move();
		void resize();
		double getArea();
};

- Anonymous April 23, 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