Amazon Interview Question for SDE1s


Country: India
Interview Type: In-Person




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

can be done recursively by incrementing static variable count if node contains left or right child and then call function for left and right child

if count >1 subtree is present

- new coder July 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 votes

Why to further check for left and right by recursive calling. Just check for the root. If root has non empty left or right child then it contains a subtree else not.

- vgeek July 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@vgeek: how ?

@new coder: can you please explain further. Also, what is the time complexity of your algo ?

- yolo July 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@yolo read this : This i am pasting from wikipedia to clarify your doubt search for the same by typing trees in the wikipedia i cannot paste the link here:
A subtree of a tree T is a tree consisting of a node in T and all of its descendants in T.[c][1] Nodes thus correspond to subtrees (each node corresponds to the subtree of itself and all its descendants) – the subtree corresponding to the root node is the entire tree, and each node is the root node of the subtree it determines; the subtree corresponding to any other node is called a proper subtree (in analogy to the term proper subset).

- vgeek July 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

LOL @vgeek has a good point. The question asks if the tree has "a" subtree not "the" subtree, meaning any subtree.

- math.matt July 15, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

static int c=0;
subtree( struct node *root)
{
struct node * temp=root;
if(temp->left!=NULL || temp->right !=NULL)
{
count++;
}
else
return;
subtree(temp->left);
subtree(temp->right);
}
if(count >1)
printf("subtree present");

- new coder July 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Why to further check for left and right by recursive calling. Just check for the root. If root has non empty left or right child then it contains a subtree else not.

- vgeek July 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Yes when do we say that a tree contains a subTree.I want to say there may be some preconditions.

- Sibendu Dey July 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

How do you search for a substring in a string ? It should be present somewhere in between the string.

Similarly, the subtree should be present somewhere in the main tree.

- yolo July 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@yolo-then we can just check whether the root has children.That itself proves that it contains a subtree.

- Sibendu Dey July 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

children of root should also have children, if child node does not have child then its just a node not a tree

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

@Anonymous:-Then the question has straight-forward answer..

- Sibendu Dey July 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

First of all, the question is incomplete, unclear..

To find if a tree has sub-tree , we just need to check and left or right child of root not being NULL.

The question should no be so simple..right ..!!!!

The question should be: given a subtree, find out if a tree contains this sub tree or not..

This is the tree version of question : given a substring, find out if it is present in a string or not..!!

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

yes the question seems to be incorrect or incomplete

- Sibendu Dey July 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This can be solved in two ways:
First solution:
1. Do an in-order traversal of tree1 and store the result in auxillary array (vector in C++ so that memory can grow dynamically)
2. Repeat step 1 for tree2
3. See if tree2 aux array is in tree1 aux array (similar to strstr)

Second solution:
1. Find minimum element in tree2 (leftmost child) and take this nodes value
2. Find this value in tree1
3. Keep finding inorder successor of this node in tree1 and tree2
4. If they match continue, it not return false
5. If tree2 is done with return true, it tree1 is done before tree 2 return false

First solution works even if duplicate elements are allowed in the tree. Second may not and will require more complicated solution.

- Anonymous March 10, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public boolean containsSubtree(TreeNode root, TreeNode subTree) {
			if(subTree == null) return true;
			if(root == null) return false;
			if(root.val == subTree.val) {
				return containsSubtree(root.left, subTree.left) && containsSubtree(root.right, subTree.right);
			}
			else if(root.val > subTree.val) return containsSubtree(root.left, subTree);
			else return containsSubtree(root.right, subTree);
		}

- DavidLiu March 18, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

when do we say a tree contains a sub tree? do all nodes should have subtree or only root node??

- Anonymous July 11, 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