Microsoft Interview Question for Software Engineer in Tests






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

My algo is .
all three pairs A & B , B & C , C & A has same LCA.

- Cool August 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Approach:

Traverse the tree:

a) if C value is found return True and keep searching for B
b) When B is found in the path mark as True and keep searching for A along the same path
c) When A is also found return True and exit
d) If any of A or B or root or leaf is encountered while the other elements are not found return False. e.g if A is found when B is not still found

b_found = False

def checkpath(root):
  
  global b_found

  if not root:
    return False

  if root.val == C:
    return True

  m = checkpath(root.left) or checkpath(root.right)
  
  if m :
    if root.val == B:
      b_found = True
    elif root.val == A:
      if not b_found:
        return False
    return True
  else:
    return False

- Galileo December 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

b_found = False

def checkpath(root):
  
  global b_found

  if not root:
    return False

  if root.val == C:
    return True

  m = checkpath(root.left) or checkpath(root.right)
  
  if m :
    if root.val == B:
      b_found = True
    elif root.val == A:
      if not b_found:
        return False
    return True
  else:
    return False

- Galileo December 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

b_found = False

def checkpath(root):
  
  global b_found

  if not root:
    return False

  if root.val == C:
    return True

  m = checkpath(root.left) or checkpath(root.right)
  
  if m :
    if root.val == B:
      b_found = True
    elif root.val == A:
      if not b_found:
        return False
    return True
  else:
    return False

- Galileo December 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 3 vote

Is the foll good enough or am I missing something.Call would be Find(head,A)

Find(Node n,Node req){
   if(n==null)
      return false;
   if(req==A)
      if(n==A)
         return Find(n,B);
   if(req==B)
      if(n==B)
         return Find(n,C);
   if(req==C)
      if(n==C)
         return true;
   else
      return Find(n.left,req)||Find(n.right,req);
}

- Sathya February 02, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Perfect Code !!!!

- manju February 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thx but i think the else part should be duplicated in each if like

Find(Node n,Node req){
   if(n==null)
      return false;
   if(req==A)
      if(n==A)
         return Find(n,B);
      else
         return Find(n.left,req)||Find(n.right,req);
   if(req==B)
      if(n==B)
         return Find(n,C);
      else
         return Find(n.left,req)||Find(n.right,req);
   if(req==C)
      if(n==C)
         return true;
      else
         return Find(n.left,req)||Find(n.right,req);
}

- Sathya February 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Just Remove else from your first code and write last statement without else!!!


Find(Node n,Node req){
if(n==null)
return false;
if(req==A)
if(n==A)
return Find(n,B);
if(req==B)
if(n==B)
return Find(n,C);
if(req==C)
if(n==C)
return true;

return Find(n.left,req)||Find(n.right,req);
}

- manjesh.bits February 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

i think the question also wants to find paths like these

B
A C

which doesnt work with the above code..

- February 04, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

how ur eg is a path from A to B then to C...its just a path from B to A or B to C isn't it?

- Sathya February 04, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What's the complexity of your solution ?

- Anonymous February 04, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Sathya - I agree with V . I think this should be the solution . First find Least common ancestor of A and C(say D). Now search for B in the path from D to A and D to C. If found in any of the path then answer is true.

- Decipher February 04, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Decipher-I think we are not on the same page.So the question now becomes how do u define "Path"?

- Sathya February 05, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Sathya : I think the above solution is wrong. it doesn't handle this case:

A
       / \
      B   C

@decipher: the solution that you told (LCA one) implies that we need to check if B lies on path from LCA(A,C) to A & also form LCA(A,C) to C but that was the thing that Q originally asked us to do ie. check if a third node lies on path between two nodes :)
So we end up nowhere. Must say Infinite loop :)

- Anonymous February 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Isn't this example wrong? The Q asks for a path from A to B and B to C..

- Anonymous March 20, 2011 | Flag
Comment hidden because of low score. Click to expand.
-1
of 3 vote

bool find(node *start, node *dest){
    if(!start){
       return false;
    }
    
    if(start == dest){
       return true;
    }
   
    return find(start->left, dest) || find(start->right, dest);
}

bool Func(node *A, node *B, node *C){
   return (find(A, B)&&find(B, C));
}

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

Crap!!!
Totally useless solution. Please think carefully before posting the code. You might be wasting others time.

- Anonymous February 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I do not see any problem with the solution.

- Anonymous February 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What is wrong with this solution. Can you please give a counter example where this code will fail.

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

I think this algorithm won't hold good if
1. B is an ancestor of A
2. A and B are in different subtrees i.e. A is in the left and B is in the right.

This will work as long as B is in a subtree rooted at A i.e. if B is a direct descendant of A.

- Anonymous March 20, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

"check if there exists a path from A to C such that B lies in the path."

I think reading the problem statement, both the scenarios you mention should return false, which the above code would do.

- Anon March 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 vote

Do depth first of Binary tree and note arrival and departure time of Node .
arrival time / departure time

1 / 10
a
/ \
2/7 b c 8/9
/ \
3/4 d e 5/6


following property will hold

arrival time (A) < arrival time (B) < arrival time (C)

dep time (A) > dep time (B) > dep time (C)

- Rakesh February 03, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think it's correct to very much extent. But a small correction:

"
arrival time (A) < arrival time (B) < arrival time (C)

dep time (A) > dep time (B) > dep time (C)
" 
This doesn't take into consideration this case: C-B-A
For that we can use the above property with values(ie times of arrival and departure) of A & C swapped.

P.S: This is called Parenthesis theorem & is one of the properties imposed by DFS.

Thanks!!!

- Guess Who?? February 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 vote

//found flag 0
//found flag 1

//calling conv
Func(t,-1,a,b,c)

bool Func(tree *t,int flag,tree *a,tree *b,tree *c)
{
if(!t && flag !=1) return false;
else if(t->val == a->val)
flag=0;
else if(t->val == b->val && flag==0)
flag=1;
else if(t->val == c->val && flag==1)
return true;
else
return(Func(t->left,flag) || Func(t->right,flag))


}

- surender February 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The above code is not working. Its giving true even if the A,B,C are not in the path

- Shivani February 06, 2011 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 vote

template <typename T>
bool checkPath(BTNode<T> *t, int lookfor, T a, T b, T c) {
  if (t!=NULL) {
    T d = t->data;
    if (lookfor == 0) {
      int newlook = d == a ? lookfor+1:lookfor;
      if (checkPath(t->left, newlook, a, b, c)) return true;
      return checkPath(t->right, newlook, a, b, c);
    }
    else if (lookfor == 1) {
      int newlook = d == b ? lookfor+1:lookfor;
      if (checkPath(t->left, newlook, a, b, c)) return true;
      return checkPath(t->right, newlook, a, b, c);
    }
    else {
      if (d == c) return true;
      if (checkPath(t->left, lookfor, a, b, c)) return true;
      return checkPath(t->right, lookfor, a, b, c);
    }
  }
  return false;
}

- amshali February 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

public bool FindPath(int[] path)
{
return FindRecursive(_head, path, 0);
}

bool FindRecursive(TreeNode current, int[] path, int currentIndex)
{
if (current != null)
{
if (current.Value == path[currentIndex])
{
if (currentIndex == path.Length - 1)
{
return true;
}
currentIndex++;
}
return FindRecursive(current.Left, path, currentIndex) || FindRecursive(current.Right, path, currentIndex);
}
return false;
}

- YN April 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

I am probably wrong but does the following approach work
1) Find the "Lowest Common Ancestor" of A and C
2) Modify this algorithm to keep track whether B is encountered on the path selected.

- DarkKnight July 19, 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