Microsoft Interview Question for Software Engineer in Tests






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

Just to be clear: are we talking about a binary tree, a BST, or any tree in general?

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

in any case,
Method 1 : memory inefficient, time + code efficient.
1. take the inorder traversal of both the trees.
2. find the longest subsequence in the two arrays.
3. if one of the array is entirely the longest subsequence, you got it, else not.

Method 2: memory efficient, time inefficient.
1. search for the root of one tree in the other.
2. if found try to match the structure, from there, using inorder as well as preorder traversal.

- luvtheedragon August 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

method 1 doesn't work all all
consider below two tree
A
B C
D E
in order is DBEAC
for tree as
E
B A
in order is BEA
can u say that is a sub-tree

- anonymous August 04, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What is the shape of the larger tree?
Is it A root, B and C direct children, then D child of B and R child of C?
If so the inorder is DBECA

- shani August 10, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Oops! Read above R child of C as *A* child of C

- shani August 10, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

anonymous is right.
How about serialize two trees and compare them?

- jimmyzzxhlh August 25, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think by serialization you mean storing traversal. For which we can use Euler's traversal (otherwise we have to compute both inorder and preorder/postorder and store it).

- Hinax September 06, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think second method is more efficient.
@luvtheedragon: could you explain how method 2 is less time efficient than method 1.

As per my understanding
suppose if Bigger tree contains N node and smaller tree contains M node.
as per first method we will traverse both the trees that would cost us

O(M+N)

then we will also traverse both the array for finding longest common match...
which will also cost O(M+N)


while in method 2:

we search the root of smaller tree in bigger tree : O( Log N)
once found we traverse both the tree to compare each node. a pre order traversal O(M)

so method would cost ( M+ Log N )

please correct me folks if i am wrong

- Crime_Master_GoGO August 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

1) Is it true that after inorder flattening, the smaller tree is a substring of the larger tree? If so - O(N+M) Boyer Moore non-trivial substring match.
2) Second method: May compare almost all the smaller tree and fail, repeating for each of the N nodes (assuming duplicates are allowed)- O(N*M).
* There are optimizations as in substring matching - Google for "subtree matching"

- shani August 10, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@Crime_Master_GoGO
How can you search a node in a binary tree (not BST) in O(log N)

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

may sound absurd but does a subtree
1. Nodes are same ? (ReferenceEqual)
2. Or just check if Data part of the node arranged in same order? (DataEqual)

If its #1 it can be found in O(n). Just traverse the bigger tree and return if you find the root of smaller tree found (referenceEqual).

For #2 Method 2 from luvtheedragon's solution

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

1. Write an Equals function that iteratively compares if 2 trees are equal
2. For the parent tree, for each node, call Equals till no nodes are left or Equals is true.

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

Some Java code I just wrote for a BST:

public boolean hasSubTree(BinarySearchTree sub) {
// Try to find head of subtree
TreeNode subHead = this.findNode(sub.root);
if ( subHead == null ) {
System.out.println("Tree head: " + sub.root.data + " NOT found!");
return false;
}

// Ok we found the head let's step on node and see if it matches
return this.navigateAndCompare(subHead, sub.root);
}

private boolean navigateAndCompare(TreeNode n1, TreeNode n2) {
if ( n1 == null && n2 == null ) {
return true;
}
if ( n2 != null && n1 == null ) {
return false;
}
if ( n1 != null && n2 == null ) {
return false;
}
if ( n1.data.equals(n2.data) ) {
boolean left = this.navigateAndCompare(n1.left, n2.left);
boolean right = this.navigateAndCompare(n1.right, n2.right);
if ( left && right ) {
return true;
}
return false;
} else {
System.out.println(n1.data + " does NOT match " + n2.data);
return false;
}
}

private void l(Object o) {
System.out.println(o);
}

public TreeNode findNode(TreeNode node) {
return this.findNode(this.root, node);
}

public TreeNode findNode(TreeNode n, TreeNode node) {
if ( node == null ) {
return null;
}
while ( n != null ) {
if ( n.data.equals(node.data) ) {
return n;
}
if ( node.data.compareTo(n.data) < 0 ) {
return this.findNode(n.left, node);
} else if ( node.data.compareTo(n.data) > 0 ) {
return this.findNode(n.right, node);
}
throw new RuntimeException("It should never get here!");
}
return null;
}

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

// returns true if n2 is subset of n1

bool isSubset( Node* n1, Node* n2 )
{
    if( n1==0 && n2!=0 )
    	return false;

    if( (n1!=0 && n2==0) || (n1==0 && n2==0) )
    	return true;

    return ( isSubset(n1->right, n2->right) && isSubset(n1->left, n2->left) );
}

- erm October 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

please ignore the above solution.
I just realize that I misunderstood the question..

- erm October 26, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

can we map the tree with larger depth to hash table
and then search the sencond one in that table?????

plz comment!!!!

- poojachitrakar1508 December 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

can we map the tree with larger depth to hash table
and then search the sencond one in that table?????

plz comment!!!!

- poojachitrakar1508 December 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Compare the tree structures if root has been found. Let the tree in which we are looking for a subtree be called super tree and tree to look for be subtree.

// superNode: node in the super tree
// subNode: node in the sub tree
// originalSubNode: original node in subtree i.e. root of subtree we are checking
bool IsSubTree(TreeNode superNode, TreeNode subNode, TreeNode originalSubNode)
{
   if ((superNode == null)&&(subNode == null))
   {
       return true;
   }

   if ((superNode == null)||(subNode == null))       
   {
       return false;
   }

   if (superNode.Key == subNode.Key)
   {
      bool leftResult = IsSubTree(superNode.Left, subNode.Left, originalSubNode);
      bool rightResult = IsSubTree(superNode.Right, subNode.Right, originalSubNode);
      
      if (leftResult && rightResult)
      {
          return true;
      }
   }

   // Either root is not equal or subtree was not found, try in subtrees
   return IsSubTree(superNode.Left, originalSubNode, originalSubNode) ||
          IsSubTree(superNode.Right, originalSubNode, originalSubNode);
}

bool IsSubTree(TreeNode superRoot, TreeNode subRoot)
{
    return IsSubTree(superRoot, subRoot, subRoot);
}

The key is to have originalSubNode so if a search fails, one can start fresh search in the subtree of super tree!

- Ashish Kaila June 20, 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