Amazon Interview Question for Software Engineer / Developers


Team: Kindle-Periodicals
Country: India
Interview Type: In-Person




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

O(n^2) approach will be Top down approach

Int MaxBST(node *root)
{
if(!root)
return 1;

if(IsBst(root)) //
return Size(root);

else
return Max(MaxBST(root->left),MaxBST(root->right));

}

The second approach will be O(n) bottom UP
/*
root is the root of tree
min minimum value of tree rooted at root
max max value of tree rooted at root
p assign the value root which contains largest bst
return maxm no of largest BST
or return -1 if tree rooted at root is not BST
*/
Int LargestBST(node *root,int &min,int &max,int &maxnoofnodes, Node **p)
{
if(!root)
return 0;

bool Isbst=true;

int leftnodes = LargestBST(root->left,min,max,maxnoofnodes,&p)

int currmin = (leftnodes==0) ? root->data : min

if(leftnodes == -1 || (leftNodes != 0 && root->data < max))
Isbst = false;


int rightnodes=LargestBST(root->right,min,max,maxnoofnodes,&p)

int currmax= (rightnodes==0) ? root->data : max

if(rightnodes==-1 || (leftNodes != 0 && root->data > min))
Isbst=false;

if(Isbst)
{
min=currmin;
max=currmax;
int total=leftnodes+rightnodes+1;
if(total > maxnoofnodes){
maxnoofnodes=total;
p=root;}
return Maxnoofnodes;
}
else
return -1;
}

- NaiveCoder March 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

In the O(n^2) approach aren't you counting the NULL'd left and right children of the leaf node as 2 nodes in the BST? I think the 'recursion stopping if' should read:
if(root->left == NULL && root->right == NULL) return 1;
No?

- just_passing_through March 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

What if we do an inorder traversal and then find the largest increasing sequence. The length of this sequence will be the biggest BST of that Binary Tree.
Space O(n)
Time: O(n) + O(n)

- King@Work March 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

That's what I wanted to suggest as well, but the problem statement reads 'find the biggest BST', not the biggest BST size. Are we supposed to find the root node as well? Or really just the size?

- just_passing_through March 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

'find the biggest BST' What does that mean? Biggest in terms of what?

- Anonymous March 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. We can find the longest sequentially increasing sub-sequence of the inorder traversal of the tree. O(n)
2. No we need to find the max value in this array - which is going to be the last element and also find the min element - which is going to be the first element. O(1)
3. We now find the least common ancestor of these two elements O(n). Call it root.
4. There after we call printAllAncestors(root.left) and printAllAncestors(root.right). O(n).


The complexity is O(n) for the algo.
Thanks.

- Arvind March 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

^ Excellent!

- Ananth March 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice, but dont we need to find Longest increasing sequence instead of Longest increasing Sub-sequence ?

- marisha March 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Arvind , this will not work in some test cases .

eg 6,7,8,9,10 are 5 numbers .
10 is root , 7 is left child and 9 is right child of 10 and
6 is left child and 8 is right child of 7 .
here inorder seq is 6,7,8,10,9 .
So according to above given algo , longest sequence of BST is 4 but its actually 3 .

- Abhi March 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Abhi,

for your example, the largest bst is of size 4.
root is 10, left child is 7.
and 7's left child is 6, right child is 8.

- Anonymous March 28, 2012 | Flag


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