Microsoft Interview Question for Software Engineer / Developers


Country: India
Interview Type: Phone Interview




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

community.topcoder.com/tc?module=Static&d1=tutorials&d2=lowestCommonAncestor
follow this....

- Anonymous January 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Or just look at any of the 10 times this problem has already been solved on this site.

- eugene.yarovoi January 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Node *FindLCA(Node *root, Node *first, Node *second)
{
  if(root==NULL)
    return NULL;
  if( (root == first) || (root == second) )
    return root;
  Node *L = findLCA(root->left, first, second);
  Node *R = findLCA(root->right, first, second);
  if ( L && R )
    return root;
  else if (L)
    return L;
  else if (R)
    return R;
}

- Rayden January 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Rayden i can't understand your logic . could you please explain more..

- Anonymous January 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

- If the root is one of the nodes searched for than it means the root is the least common ancestor.

- If the two nodes are found in left and right subtrees seperately it again means the root is the LCA.

- Rayden January 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

what if [*second] is a child of [*first]?

- sammy January 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

That case is already covered.

- Rayden January 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int lca(Node *Root, int n1, int n2){
    int found = 0;
    if(Root == NULL) return 0;
    if(Root->data == n1 || Root->data == n2)
                  return 1+lca(Root->left, n1,n2)+lca(Root->right, n1,n2);
    found = lca(Root->left, n1,n2)+lca(Root->right, n1,n2);
    if(found == 2){
             printf("\n%d\n", Root->data);
             return 0;
    }
    return found;
}

- SM January 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int data1;
int data2;
node* LCS(node *root, bool *firstfound, bool *secondfound)
{
if(root == null ) return null;
node *left = LCS(root->left, firstfound, secondfound);
node *right = LCS(root->right, firstfound, secondfound);
if(left > 0 || right > 0)
{
return(left > 0 ? left : right);
}
if(left < 0 && right < 0)
{
return root;
}
else if (left <0 || right < 0)
{
if(firstfound && root->data==data2)
{
return root;
}
else if(secondfound && root->data == data1
{
return root;
}
else
{
return -1000;
}
}
else
{
if(root->data == data1)
{
*firstfound = true;
return -1000;
}
else if (root->data == data2)
{
*secondfound = true;
return -1000;
}
}
return null;
}

- Anonymous January 26, 2012 | 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