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; }

- lesnar June 24, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- Neeraj September 16, 2011 | Flag
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.

- Anonymous June 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- ashok.koyi September 19, 2011 | Flag
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 .

- Anonymous September 19, 2011 | Flag Reply
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; }

- Anonymous December 21, 2011 | Flag Reply
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.

- Amit Priyadarshi March 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

what are small x and y in this algos

- shreyans June 17, 2012 | Flag Reply
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 .

- systemic emotions November 30, 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