Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

traverse the tree in INorder , and count till the ascending order sorting order fails!

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

Adding to Gpoi's comments. May be the longest contiguous increasing sub sequence will do.

- Dr.Sai December 05, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

this is not correct one... see below example
       10
       / 
      5
       \
        2
the inorder starts with 5 and then 2 it fails here and returns, but 10 and 5 is the biggest BST. How you get this one?

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

Hi,
Can we say 5 and 10 are in BST ? as 5 is not satisfying the BST property (the child node of 5 is violating this rule)

- Gopi December 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

There can be two cases.
(1) One in which the subtree should completely be a BST.
(2) Other in which a part of subtree can be a BST.
So which one is being referred ?

(1)

bool findLargestBST(tree *root, int *c)
{
if(root == NULL)
{
return true;
}
int x = 0;
int y = 0;
bool p = findLargestBST(root->left, &x);
bool q = findLargestBST(root->right, &y);

if(root->left)
root->min = root->left->min;
else
root->min = root->data;

if(root->right)
root->max = root->right->max;
else
root->max = root->data;

if(p && q && (root->left == NULL || root->data > root->left->max) && (root->right == NULL || root->right->min > root->data))
{
*c = (x + y + 1);
return true;
}
*c = max(x, y);
return false;
}

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

I haven't looked over your code, but good job on identifying the ambiguity.

- eugene.yarovoi December 05, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Nice article on this (remove spaces)

ihas1337code . com/2010/11/largest-binary-search-tree-bst-in.html

- swathi December 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Well I would outline a simple solution in this way:-
Have two variables, depth (integer) = 0 and largestBSTRoot (Reference/Ptr to Node class) = NULL.

1) Query each node whether it is a root of a BST. This means that both its left and right subtrees must be BSTs and left node must be larger than it and right node smaller. If it is a leaf node it is trivially a BST.

2) Whenever a node finds that it is a root of a BST, check if its height is larger than the value of "depth". If it is, replace largestBSTRoot with ptr/ref to node.

Time Complexity is O(n) as we are doing DFS, Space complexity is O(n) as well due to the bloody recursion.

- Parth_Mehta_UIC December 13, 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