Interview Question for Applications Developers


Country: India
Interview Type: In-Person




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

let inorder traversal of Tree is S1
inorder traversal of SubTree is S2
pre order traversal of Tree is S3
pre order traversal of Sub Tree is S4
now check if s2 is substring of s1 and s4 is substring of s3

- ashwitha93 June 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

why both inorder and preorder? can you think of an example where just inorder wont be enough?

- getprith June 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Inorder & PreOrder take O(n) time.
After that substring search takes O(n) time with KMP algorithm.

So in effect, O(n) time we could verify this.

- X June 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Huh? This is crazy. Won't work.

- Anonymous June 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@getprith... doing only the inorder will not work because different structures of a tree may result in same inorder, which wont be considered as subtree because it
should be same regarding structure too. (same for only preorder )

- Zzenith June 26, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

A counter-example
     A
   /  \
  B    C
 / \    \
D   E    F
    
PreOrder: A B D E C F
InOrder: B D E A C F
   
     A
   /  \
  B    C
   \    
    E    
   
PreOrder: A B E C
InOrder: B E A C

- Anonymous June 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Anonymous: A subtree is defined as
A subtree of a tree T is a tree consisting of a node in T and all of its descendants in T.
In the example you have given, the second tree is not a subtree of the first.

- pooja.gator July 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

For example, in the following case, tree S is a subtree of tree T.

Tree S
10
/ \
4 6
\
30


Tree T
26
/ \
10 3
/ \ \
4 6 3
\
30



/* Check if the data of both roots is same and data of left and right
subtrees are also same */
return (root1->data == root2->data &&
areIdentical(root1->left, root2->left) &&
areIdentical(root1->right, root2->right) );
}


/* This function returns true if S is a subtree of T, otherwise false */
bool isSubtree(struct node *T, struct node *S)
{
/* base cases */
if (S == NULL)
return true;

if (T == NULL)
return false;

/* Check the tree with root as current node */
if (areIdentical(T, S))
return true;

/* If the tree with root as current node doesn't match then
try left and right subtrees one by one */
return isSubtree(T->left, S) ||
isSubtree(T->right, S);
}

- priya September 09, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

I think we need to consider any two traversals.
Like inorder & preorder for both and then checking subsring.
OR
inorder & postorder
OR
preorder & postorder

- subahjit June 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i wasn't able to think of anything in the interview room but after coming out i realized.

{
public class BinaryTree {
	Integer content;
	BinaryTree left;
	BinaryTree right;

	@Override
	public String toString() {
		// program to return a string representation of this tree;
		return content + ((left == null) ? "" : left.toString())
				+ ((right == null) ? "" : right.toString());
	}
}

}
if you toString both Trees you can just check if one is a substring of the other

- getprith June 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

By subtree, shouldn't the subtree structure be also taken into consideration?

- subahjit June 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

but if we have the same ordering while traversing both the trees wouldnt that be enough?

- getprith June 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. Search the root node of the subtree in the big tree.
2. For every node found,
2.a. traverse at the same time to see if the subtree matches the subtree found in the main
2.b. if found return true
3. Return false (not found)

- math.matt June 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The question doesn't say anything about any specific tree, son the assumption of the binary tree cannot be justified. An O(n) time, O(n) space algorithm would be :
1.Hash the tree nodes
2. Hash the subtree in the same hash and if there is any subtree node whose entry is not occupied, return 0 (i.e., not a subtree)
* n is the total number of nodes in the original tree

- Anonymous June 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

the tree initially was not a tree. but looking at me struggle the guy made it a binary tree. can we see some code with your solution implemented?

- getprith June 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can check for inorder traversal of both the tree simultaneously within the same function

Let us suppose we iterate the main tree in preorder traversal such that we have the head node of the subtree and the current node of the main tree to have the same values.

Now call the following function

bool SearchSubtree(tree * HeadMain , tree* HeadSubtree) // HeadMain->value = HeadSubtree->value

{
 If (!HeadSubtree) 
    return true;

 if ((HeadMain == NULL ||  HeadMain != HeadSubtree) && (HeadSubtree != NULL))
    return false;

if ((SearchSubtree(HeadMain->left , HeadSubtree->left)) == false)
    return false;

return (SearchSubtree(HeadMain->left , HeadSubtree->left)) == false) ;

}

So what I'm trying to do is
1)Check if the nodes do not match , subtree is not present
2)If the nodes match , check the left subtree of both
3)Then check the right subtree
4)Now we dont have to traverse the Main tree completely . Eg. We'll keep on calling the left child of the main tree recursively only to the point the left child of the subtree has been completely traversed . We recursively call back the previous nodes for both . In essence we are traversing only that part of the main tree which is concerned with the subtree .
5)Extra memory is saved from copying each elsement and checking them later
6)The complexity O(N) where n corresponds to the number of elements of the subtree

- sumit.083 June 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1.Hash the elements of the sub-tree (this save us some space if the original tree is very large)
2. Start mapping the elements of the original tree and increment the counter every time there is a match.
3. return true if counter=size_of_sub_tree, return false otherwise

- Anonymous September 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

just check

boolean foo(binNode root, binNode subtree_root){
	if(root!=null){
		if(root!=subtree_root)
			return foo(root.right,subtree_root)||foo(root.left, subtree_root);
		else return true;
	}else return false;

}

- Anonymous June 24, 2013 | 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