Interview Question






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

bool LCA (node *r, int v1, int v2)
{
    if (r == NULL) return FALSE;
    BOOL fFoundInLeft = LCA (r->left, v1, v2);
    BOOL fFoundInRight = LCA (r->right, v1, v2))
    if (fFoundInLeft && fFoundInRight)
    {
         print ("found LCA = %x", r);
         return TRUE;
    }
    if (fFoundInLeft) ||fFoundInRight)
    {
         if ((r->v == v1) || (r->v == v2))
              print ("found LCA = %x", r);
         return TRUE;
    }
    if ((r->v == v1) || (r->v == v2))
         return TRUE;
    return FALSE;
}

- MS June 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hey thanks for the answer:
Just one small doubt:
if (fFoundInLeft) ||fFoundInRight)
{
if ((r->v == v1) || (r->v == v2))
print ("found LCA = %x", r);
return TRUE;
}
IMO this will hit only when either of v1 or v2 is root, in that case we should return LCA not found or are we considering a node is a ancestor of itself.

Also about the complexity analysis the complexity is O(n). Isnt it possible to give a solution with o(logn) complexity.

For example we can use marking algorithm:

1) traverse from v1 to root and mark every node.
2) then traverse from v2 to root and the first marked node is our LCA if the marked node is not v1.
3) or else parent of v1 will be LCA.

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

Step1 Get Inorder traversal of binary tree
Step2 LCS(Node *root,int Val1,int Val2)
Step3 If(Val1 and Val2 occur before root in inorder traversal)
then LCS(root->Left,val1,val2)
else if(both Val1 and Val2 occur to the right of root in inorder traversal)
then LCS(root->right,val1,val2)
else if(both val1 and val2 are present in binary tree)
then return root
else print(NO COMMON ANCESTOR AS VALUES ARE NOT PRESENT IN THE TREE)

Comments over the solution will be highly appreciated.

- 707.rohit March 19, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good one

- jiex.cs November 30, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What if one of the values is 2 or 3 levels down the current level in the left..?? how would it work then. What you have suggested might work for binary Search Tree, i doubt if this would work for a simple binary tree...

- Srivathsan March 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why wouldnt it work for simple binary tree..Plz have a dry run and do come with some example where u think it wouldnt work.Dont just assume the things.

- Anonymous March 12, 2012 | Flag


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