Adobe Interview Question for Software Engineer / Developers






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

Suppose ABC is triangle and P is given point.

Step 1. Find Area(PAB), Area(PBC), Area(PCA)
Step 2. If all three areas are of same sign (irrespective of positive or negative), then P lies inside ABC, otherwise outside

Note: Better than Sanjay's approach because it doesn't calculate area of whole triangle

To see how it works:
1. When you find area of PAB, and if it is positive, it means you are standing at P and A, B are clockwise from P.
2. Similar argument holds for PBC and PCA.
3. If all three areas are positive then you are standing at P and looking at A, B and C in clockwise direction, if all are negative then anti-clockwise.
4. If there is a sign change, you (P) are outside triangle ABC.
5. If any of the area is zero, then P is one of the edges AB, BC or CA

- mag February 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 3 vote

One more way...

Find the area of triangle and three triangle's joining three point's taking two point from the triangle and one given point... if the area of original triangle is equal to sum of all three triangle then it is inside the triangle, if any one smaller triangle area is zero the on the triangle else outside..

- Sanjay July 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The area comparison given by @Sanjay seems to be the easiest, and the cleanest solution.

- Shivendra Tiwari August 01, 2011 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Good solution.

geeksforgeeks.org/archives/22146

- Psycho September 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If the point is inside the triangle, then the line connecting one vertex with the point should intersect the line connecting the other two vertexes. Check this property for all three vertexes. If all hold, then the point lies inside.

- Hey July 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

one more point to be noted that both the points(vertex and the given point) will lie on the same side of the intersecting edge of the triangle.

- bibhay.vishesh July 20, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

what about (0,0) (1,0) (0,1) and point is (-1,-1) or (1,1)

- Sanjay July 20, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

My Bad.. you have wirtten all three vertices .. then it will work.. but the algo is very complex..

- Sanjay July 20, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

make one point as a origin such as both point lies at positive side of x axis... then for the new co-ordinate... put the point in the equation of line joining the three point's..
for origin to first and second point it should be positive and negative... or vice versa..
less then zero for line joining the non zero two points..
then its inside the triangle...
otherwise no

- Sanjay July 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Another Solution - We can find the centroid of the triangle and then check on which side of each side the Point to be tested lies when compared with that of the centroid.

- kewl_coder August 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="" line="1" title="CodeMonkey51728" class="run-this">#include<iostream.h>
#include<conio.h>
#include<math.h>
#define cosi(x1,y1,x2,y2) acos((x1*x2+y1*y2)/sqrt((pow(x1,2)+pow(y1,2))*(pow(x2,2)+pow(y2,2))))
void main()
{
clrscr();
int i,f;
char ch='c';
double q1,q2,q3,p[3][2],p1[2],a1,a2;
cout<<"enter the coordinates of triangle\n";
for(i=0;i<3;i++)
cout<<"\rx=",cin>>p[i][0],cout<<" y=",cin>>p[i][1];
do
{
clrscr();
cout<<"\nenter any point";
cout<<"x=",cin>>p1[0],cout<<" y=",cin>>p1[1];
i=0;
f=0;
while(i<3)
{
if(p1[0]==p[i][0]&&p1[1]==p[i][1])
{
f=1;
break;
}
i++;
}
if(f==0)
{
for(i=0;i<3;i++)
{
f=1;
a1=1;
a2=-1;
if(i==1)
a2=2;
else
if(i==3)
a1=-2;
q1=cosi((p[i][0]-p[i+a1][0]),(p[i][1]-p[i+a1][1]),(p[i][0]-p[i+a2][0]),(p[i][1]-p[i+a2][1]));
q2=cosi((p[i][0]-p1[0]),(p[i][1]-p1[1]),(p[i][0]-p[i+a1][0]),(p[i][1]-p[i+a1][1]));
q3=cosi((p[i][0]-p1[0]),(p[i][1]-p1[1]),(p[i][0]-p[i+a2][0]),(p[i][1]-p[i+a2][1]));
if(abs(q1)<(abs(q2)+abs(q3)))
{
f=0;
break;
}
}
}
if(f==1)
cout<<"\ninside the triangle";
else
cout<<"\noutside the triangle";
cout<<"\n\n\n press q to quit";
ch=getch();
}while(ch!='q');
getch();
}
</pre><pre title="CodeMonkey51728" input="yes">
</pre>

- BIPLOVESARKAR August 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Algo:

1. take first line (by vertices 1 and 2) and solving it with given point should give same sign as with vertex 3.

2. take second line (by vertices 2 and 3) and solving it with given point should give same sign as with vertex 1.

3. take third line (by vertices 3 and 1) and solving it with given point should give same sign as with vertex 2.

if all of the above is satisfied line is in the triangle otherwise its not.

- Lalit September 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

oh! Sanjay gave a simpler solution

- Lalit September 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Take one vertex of a triangle and one of the lines connecting to it. Draw a line from vertex to point you want to scrutinize. Check the angle between two lines and compare to angle of the triangle for that vertix. Repeat for other vertices. If the angle with the point is smaller than angle of the triangle for all three vertices, point is inside the triangle.

- Puchatek December 03, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

let L1,L2,L3 are 3lines of triangle
then calculate
p(x,y)=L1*L2*L3;
if p(x1,y1)<0 point is inside
if p(x,y)==0 point is at boundry
else point is outside

- mohit February 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

atleast one "x" co-ordinate of the three points of the triangle should be greater than the "x" co-ordinate of the point inside the triangle, if this happens check the same condition for "y" co-ordinate., then the point lies inside the triagle
otherwise not

am i missing something ???

- shifu July 18, 2013 | 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