Amazon Interview Question for Software Engineer / Developers






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

class Node {
public:
Node * left;
Node * right;
Node * parent;
};
leftflag = 0;
rightflag = 0;

Traverse thru right subtree of y and search for z, if found set right flag as 1

Else traverse thru parent/grandparent of y until the parent node is not root
For each parent/grandparent greater than y - Search thru the right subtree of parent
even at root - search thru right subtree of root. If z found set rightflag as 1

If right flag is 0 exit with FALSE

Else Traverse thru left subtree of y and search for x, if found set left flag as 1

Else traverse thru parent/grandparent of y until the parent node is not root
For each parent/grandparent less than y - Search thru the left subtree of parent
even at root - search thru left subtree of root. If x found set leftflag as 1

if leftflag = 0, exit with FALSE status

exit with TRUE status

- d May 18, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In most interviews parent node pointer is not allowed. :D
In-order traversal from X,
if we meet Z first,
then return false;
else if we meet Y first,
then In-order traversal from Y.
if we meet Z, then return true;
else return false;
else return false;// meet neither
Of course, it's possible that Z is ancestor of X. Repeat.

- Anonymous May 18, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can a run of BFS or DFS do the job

- vkatharki May 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Cant we do this ?
Identify the node that is closer to the root . let it be x.
now search for y in he subtree rooted at x. if not found return false.
Then search for Z in the subtree rooted at y. If found return true else return false

- Anonymous May 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

p
    q   x
   y
 z

fails

- heartbroken May 21, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This one is closest.

But don't need to identify the node that is closer to the root.

Simply search subtree of x, if y is not found, return false

if y is found, search subtree of y, if z is found, returen true; else return false

- Yue May 21, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

It might work ...

find_ele_inpath(x,y,z) {

if (x == y && x == z) {
return(1); //path exists
}

else if (x == y) {
return find_ele_inpath(x,z,z);
}

else if (x == z) {
return(0);
}

find_ele_inpath(x->left, y, z);
find_ele_inpath(x->right, y, z);

}

- Div May 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

boolean findElementPath ( node x , node y, node z, bool bx, bool by, bool bz, node t)
{
   if( !bx && t == x ) bx = true ; 
   else if ( !by && t == y ) by = true ; 
   else if ( !bz && t == z ) bz = true ; 
   else return true ; 
    
   return ( findElementPath (x,y,z,bx,by,bz,t->left) || 
            findElementPath (x,y,z,bx,by,bz,t->right) )

}

- Anonymous May 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

forgot one thing ...

boolean findElementPath ( node x , node y, node z, bool bx, bool by, bool bz, node t)
{
  if (t == NULL ) return false ;  
  if( !bx && t == x ) bx = true ; 
   else if ( !by && t == y ) by = true ; 
   else if ( !bz && t == z ) bz = true ; 
   else return true ; 
    
   return ( findElementPath (x,y,z,bx,by,bz,t->left) || 
            findElementPath (x,y,z,bx,by,bz,t->right) )



}

- Anonymous May 22, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Anonymous little modification to your code, check it out

boolean findElementPath ( node x , node y, node z, bool bx, bool by, bool bz, node t)
{
if (t == NULL ) return false ;
if( !bx && t == x ){ bx = true ;}
else if ( bx && !by && t == y ) {
by = true ;
findElementPath ( node x , node y, node z, bool bx, bool by, bool bz,t->right);
}
else if ( bx && by && !bz && t == z ){ return true ;}
else{
return ( findElementPath (x,y,z,bx,by,bz,t->left) || findElementPath (x,y,z,bx,by,bz,t->right) );
}


}

- guest123 May 29, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Anonymous little modification to your code, check it out

boolean findElementPath ( node x , node y, node z, bool bx, bool by, bool bz, node t)
{
if (t == NULL ) return false ;
if( !bx && t == x ){
bx = true ;
findElementPath ( x ,y,z,bx, by,bz,t->right);
}
else if ( bx && !by && t == y ) {
by = true ;
findElementPath ( x ,y,z,bx, by,bz,t->right);
}
else if ( bx && by && !bz && t == z ){
return true ;
}
else{
return ( findElementPath (x,y,z,bx,by,bz,t->left) || findElementPath (x,y,z,bx,by,bz,t->right) );
}

}

- guest123 May 29, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

findElementPath ( x ,y,z,bx, by,bz,t->right);

Why we are passing t->right.

- Guest June 02, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

There is a fundamental question here -

Do we want to be able to detect patterns of only x - y - z ? or are we also interested in patterns z - y - x as well ? If we are, then the above approaches will not work. YOu will have to add another function that checks for subtrees of z for y, and then subtrees of y for x. This case will not be detected by the initial algorithm.

- selekt June 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

There is a fundamental question here -

Do we want to be able to detect patterns of only x - y - z ? or are we also interested in patterns z - y - x as well ? If we are, then the above approaches will not work. You will have to add another function that checks for subtrees of z for y, and then subtrees of y for x. This case will not be detected by the initial algorithm.

- selekt June 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

For example, the above will fail for -

y
   / \
  z   x

- selekt June 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about a variant of Preorder which keeps track of encountered nodes in two bools - left and middle. The trick is to reset the bools - left and middle when we leave a node encountered.

void ModifiedPreOrder(node* root, bool& sucess)
{
   static bool left = false;
   static bool right = false;

   if(node != NULL)
   {
      if(root->data == X)
         left=true;
  
     if((root->data == Y) && (left == true))
         middle = true;

     if(root->data == Z) && (middle == true)
        sucess = true;
     

       PreOrder(node->left, sucess);
       PreOrder(node->right, sucess);

      //Reset status flags when we leave this node.
       if(root->data == X)
       {
         left=false;
         middle=false;
       }

       if(root->data == Y)
          middle= false;
   }
}

- AD July 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Do a euler tour of the tree to find the LCA of the x and z. If common ancestor is y then return true otherwise false.

My main logic here is that any path between two nodes of a tree could only be through their common ancestor.

- logan August 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Correct me if I am wrong
Will this work?
To Check whether y lies between x & z

Find least common ancestor of x and y say A1
Find least common ancestor of z and y say A2

if A1==A2
return true
else
return false

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

Check my code

int FindXYZ(Tnode *root,Tnode *X,Tnode *Y,Tnode *Z)
{
    if(root==NULL)
        return 0;
    static Tnode *rootnode=NULL;
    static int found=0;
    if(rootnode==NULL)
        rootnode=root;
    int left=FindXYZ(root->left,X,Y,Z);
    int right=FindXYZ(root->right,X,Y,Z);
    if(left==1 && right==1 && found==0)
    {
        if(root==Y)
            found=1;
    }
    if((left==11 || right==11)&&found==0)
    {
        if(root==X || root==Z)
            found=1;
    }
    if(left==10 || right==10)
        found=-1;
    if(left==2 || right==2)
        found=-1;
    if(rootnode==root)
        return found>0;
    if(root==X||root==Z)
        return left+right+1;
    if(root==Y)
        return left+right+10;
    return left+right;
}

- ananthakrishnan.s.r June 26, 2011 | 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