## Flipkart Interview Question for Software Engineer / Developers

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

I know this uses a even-odd algorithm ... but i dot not completely understand how this code works in the if () condition....can anyone explain ?

bool pointInPolygon() {

int i, j=polySides-1 ;
boolean oddNodes=NO ;

for (i=0; i<polySides; i++) {
if (polyY[i]<y && polyY[j]>=y
|| polyY[j]<y && polyY[i]>=y) {
if (polyX[i]+(y-polyY[i])/(polyY[j]-polyY[i])*(polyX[j]-polyX[i])<x) {
oddNodes=!oddNodes; }}
j=i; }

return oddNodes; }

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

It checks if the next line taken of the polygon lies to the right of the point considered. If the total is odd then inside else outside.

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

One of the vertices or inside? No matter; the answer is still binary search, with the appropriate geometric primitives.

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

Can u plz explain on how do you solve whether the given point lies inside a ploygon in O(logn)?

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

Budy there's something missing in the question . Seems like the points are already sorted. only
in that case you can get O(logn) otherwise its impossible .

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

``````bool pointInPolygon() {

int      i, j=polySides-1 ;
boolean  oddNodes=NO      ;

for (i=0; i<polySides; i++) {
if ((polyY[i]< y && polyY[j]>=y
||   polyY[j]< y && polyY[i]>=y)
&&  (polyX[i]<=x || polyX[j]<=x)) {
oddNodes^=(polyX[i]+(y-polyY[i])/(polyY[j]-polyY[i])*(polyX[j]-polyX[i])<x); }
j=i; }

return oddNodes; }``````

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

1. a point lies in a polygon if any ray intersecting it and the polygon from any direction intersects odd number of lines (pretty intuitive). this ray can be drawn from x or y axis.
2. let us draw a ray along x axis. in this case we can state that if we have odd number of lines cutting the ray (ie odd number of lines to the right of the point) then the point is interior.
3. what flowoing code does is.

for (i=0; i<polySides; i++) {
if ((polyY[i]< y && polyY[j]>=y
|| polyY[j]< y && polyY[i]>=y)
&& (polyX[i]<=x || polyX[j]<=x)) {
oddNodes^=(polyX[i]+(y-polyY[i])/(polyY[j]-polyY[i])*(polyX[j]-polyX[i])<x); }
j=i; }
if you observe. we iterate over each line and ensure that for all lines which sweep the point along y axis we have seen that the point lies on its right.

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

what are small x and y in this algos

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

But the question is not to check if the point is inside or outside , " Find if the point is one of the vertices of the polygon in O(log N) time.. " ?? Please correct me if I m wrong .

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.

### 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.