Accenture Interview Question for Java Developers


Country: United States




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

Only after solving the question did I read the title...doh! Aw, well. here's some C++!

(I included some << overloads to print out the objects if you want.)

#include <iostream>
#include <vector>
#include <cmath>

class Point {
private:
  int x, y;
public:
  Point(): x(0), y(0) {}
  Point(int x, int y): x(x), y(y) {}

  int getX() const { return x; }
  int getY() const { return y; }

  friend std::ostream& operator<<(std::ostream& stream, const Point& pt);

};

class Line {
private:
  Point pt1, pt2;
public:
  Line(): pt1(0,0), pt2(0,1) {}
  Line(const Point& pt1, const Point& pt2): pt1(pt1), pt2(pt2) {}

  friend auto operator<(const Line& ln1, const Line& ln2) -> bool;

  friend std::ostream& operator<<(std::ostream& stream, const Line& ln);
};

class Quadrilateral {
private:
  Point pt1, pt2, pt3, pt4;
public:
  Quadrilateral(): pt1(0,0), pt2(0,1), pt3(1,0), pt4(1,1) {}
  Quadrilateral(const Point& pt1, const Point& pt2,
		const Point& pt3, const Point& pt4): pt1(pt1), pt2(pt2),
						     pt3(pt3), pt4(pt4) {}

  auto getAllLines() const -> std::vector<Line> {
    std::vector<Line> quadLines;
    Line l1(pt1,pt2), l2(pt2,pt3), l3(pt3,pt4), l4(pt4,pt1);
    quadLines.push_back(l1); quadLines.push_back(l2);
    quadLines.push_back(l3); quadLines.push_back(l4);
    return quadLines;
  }

  auto longestSide() const -> Line {
    Line l1(pt1,pt2), l2(pt2,pt3), l3(pt3,pt4), l4(pt4,pt1);
    //(l4 < (l3 < (l1 < l2 ? l2 : l1) ? (l1 < l2 ? l2 : l1) : l3) ? (l3 < (l1 < l2 ? l2 : l1)) : l4);
    Line comp1 = (l1 < l2 ? l2 : l1);
    Line comp2 = (l3 < comp1 ? comp1 : l3);
    Line comp3 = (l4 < comp2 ? comp2 : l4);

    return comp3;
  }

  friend std::ostream& operator<<(std::ostream& stream, const Quadrilateral& q);

};

std::ostream& operator<<(std::ostream& stream, const Point& pt) {
  stream << "(" << pt.x << ", " << pt.y << ")";
  return stream;
}

std::ostream& operator<<(std::ostream& stream, const Line& ln) {
  stream << ln.pt1 << " <-> " << ln.pt2 << std::endl;;
  return stream;
}

std::ostream& operator<<(std::ostream& stream, const Quadrilateral& q) {
  stream << q.pt1 << "\t" << " <-> " << "\t" << q.pt2 << "\n"
	 << "  ^" << "\t" << "\t" << "  ^" << "\n"
	 << "  |" << "\t" << "\t" << "  |" << "\n"
	 << "  v" << "\t" << "\t" << "  v" << "\n"
	 << q.pt4 << "\t" << " <-> " << "\t" << q.pt4 << "\n"
	 << std::endl;
  return stream;
}

auto operator<(const Line& ln1, const Line& ln2) -> bool {
  double ln1_len = sqrt( pow((ln1.pt1.getX() - ln1.pt2.getX()),2) - pow((ln1.pt1.getY() - ln1.pt2.getY()),2) );
  double ln2_len = sqrt( pow((ln2.pt1.getX() - ln2.pt2.getX()),2) - pow((ln2.pt1.getY() - ln2.pt2.getY()),2) );
  return ln1_len < ln2_len;
}

- C++ Just Because Why Not? Okay September 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Notice, I did not add the copy constructor or assignment operators. Just the basic old constructors :)

- C++ Just Because Why Not? Okay September 01, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Point {
	private int x, y;
	public Point(int x, int y){
		this.x = x;
		this.y = y;
	}
	public Point(){
		this.x=0;
		this.y=0;
	}
	public void setX(int x) {
		this.x = x;
	}
	public void setY(int y) {
		this.y = y;
	}
	public int getX() {
		return x;
	}
	public int getY() {
		return y;
	}
}
public class Line {
	
	private final static int NOOFPOINTS = 2;
	private Point vertex[];
	public Line(Point p1, Point p2){
		vertex = new Point[NOOFPOINTS];
		vertex[0] = p1;
		vertex[1] = p2;
	}
	public float getLength(){
		float length = 0;
		float xParam = 0;
		float yParam = 0;
		for(Point p:vertex){
			if(xParam==0 && yParam==0){
				xParam = p.getX();
				yParam = p.getY();
			}
			else{
				xParam -= p.getX();
				yParam -= p.getY();
			}
		}
		length = (float)Math.sqrt(Math.pow(xParam, 2)+Math.pow(yParam, 2));
		return length;
	}
}
public class Quadrilateral {
	
	private final static int NOOFPOINTS = 4;
	private Point vertex[];
	
	public Quadrilateral(Point p1, Point p2, Point p3, Point p4){
		vertex = new Point[NOOFPOINTS];
		vertex[0] = p1;
		vertex[1] = p2;
		vertex[2] = p3;
		vertex[3] = p4;
	}

	public List<Line> getAllLines(){
		List<Line> result = new ArrayList<Line>();
		for(int i=0; i<vertex.length-1; i++){
			result.add(new Line(vertex[i], vertex[i+1]));
		}
		return result;
	}
	
	public Line longestSide(){
		Line result = null;
		float length=0;
		for(int i=0; i<vertex.length-1; i++){
			Line l = new Line(vertex[i], vertex[i+1]);
			float len = l.getLength();
			if(len>length){
				length = len;
				result = l;
			}
		}
		return result;
	}
}

- Milan September 02, 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