Amazon Interview Question for Software Engineer / Developers






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

height(BST) = 1 + min(height(BST->left),height(BST->right));

- Anurag Singh February 11, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int minBST(node * root)
{
if (node == NULL)
return 0;
else
return 1 + min( minBST(root->left), minBST(root->right) );
}

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

Try for data: 20 40 50 (one sided tree)

         20
             40
                50

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

what is wrong with that dude

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

what if we also put few constraints in the recursive function-

int minBST(node * root)
{
if (node == NULL)
return 0;
elseif ( ! node->left && ! node->right ) // No child
return 1 ;
elseif ( ! node->left ) // Only right child,no left child
return ( 1+ minBST(root->right) );
elseif ( ! node->right ) // Only left child
return ( 1+ minBST(root->left) );
else
return 1 + min( minBST(root->left), minBST(root->right) );
}

I think this should work fine even if a tree only has left children Or only has right children. Because for the tree given by Anonimous,
20
30
40
For this tree- min height should be 3, and not 1.

- Faith February 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Above approaches fail on the shown test case, one approach could be:
int count = 0;//denotes distance from the root

Do depth first traversal of tree(using a stack):
When you pop from the stack, read the nodes count, and push its children (right, left) in stack with value -> count + 1
If you have popped an node which has neither left nor right child, compare it to current min, if smaller, store it as min depth.

At the end, you will have the min depth of tree.

time complexity - O(no. of Vertex + no. of Edges)
worst case space complexity - O(V)

- gatorsPK February 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I am thinking of applying a Breadth First search on the tree. No matter what type of tree it may be.

- Karthik February 15, 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