Amazon Interview Question for Software Engineer in Tests






Comment hidden because of low score. Click to expand.
0
of 0 vote
Assuming the tree node structure is not allowed to change, we can maintain a hash table with bst-tree-node counts for each BST node. We can use preorder traversal to solve this. {{{ global_max_node_cnt =0; global_max_sub_tree_root = null; inorder(Tree *root) { if(root == null) return; if(root->left == null && root->right ==null) put(root, 1); inorder(root->left); visit(root); inorder(root->right); if(root->left->key < root->key < root->right->key) { left_bst_cnt = get(root->left); right_bst_cnt = get(root->right); if(left_bst_cnt > 0 && right_bst_cnt > 0) { put(root, left_bst_cnt + right_bst_cnt + 1); } else { put(root, 0); } else { put(root, 0); } if(global_max_node_cnt < get(root)) { global_max_node_cnt = get(root); global_max_sub_tree_root = root; } } - imran.sk December 07, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

could u post the definition of 'largest tree'?

- Anonymous December 07, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The subtree with the greatest number of nodes.

- random December 08, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I have a doubt in the question... The given tree is a BST... So if omit any 1 node... The BST still remains a BST. Why cant we return this tree as the solution. It is definitely a BST and it does contain the largest number of nodes

- abhimanipal April 08, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

My bad... I assumed that input was BST

- abhimanipal April 08, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can someone put this in compilable Java? Thanks.

- ellemeno December 09, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can use preorder traversal to solve this. Don't you mean inorder??

- Sam December 10, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

yeah it should be inorder, but preorder might be required to construct back the tree
1) find inorder I and preorder P O(n)
2) find longest continuos increasing subsequence from I O(n)
3) finding the root from the sequence using I and P. this is classic build tree using preorder/inorder.

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

seems good

- Yoda February 23, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

node * ReturnMaxSubBst(node *root)
{
   int lcount = count(root->left);
   int rcount = count(root->right);
   if(lcount >= rcount)
       return isBst(root->left)?root->left:NULL;
   else
       return isBst(root->right)?root->right:NULL;
}

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

int isBst(node *root)
{
   if(root == NULL) 
      return true;
   if(root->left != NULL && min(root->left) > root->data)
      return false;
   if(root->right != NULL && max(root->right) <= root->data)
      return false;
   if(!isBst(root->left)&&!isBst(root->right))
      return false;
   return true;
}

node* min(node *root)
{
   while(root != NULL)
       root = root->left;
   return root;
}

node* max(node *root)
{
   while(root != NULL)
       root = root->right;
   return root;
}

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

Nice solution. max(), min() should return data type, instead of node*.

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

Very good Solution

- Anonymus February 23, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

just a small typo...isn't the isBst() return type bool?
bool isBst()?

- Anonymous February 23, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I do not understand how this is a correct solution. There are several issues here -

1. Even when the size of the right subtree is greater than that of the left or vice versa, there is no guarantee that the largest BST is going to exist in right/left. It can be in either subtree.

2. After counting the # of nodes, it is simply checking from the root, which sub tree is actually a BST. It is not checking the subtrees of subtrees for BSTs.

If retirnMaxSubBST is recursively called for every node in the tree, only then can this solution work. Also, the function does not return the number of nodes in that tree either its root.

- Anonymous May 23, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yeah what you said is correct , let me know if the following recursive program will work.

public Node LargestBST(Node currNode, Node maxNodeBST) {
        if (currNode == null) {
            return null;
        }
        boolean isBST = isBST(currNode);
        int size = size(currNode);
        int sizeOfMaxBst = size(maxNodeBST);
        if (isBST && sizeOfMaxBst < size) {
            maxNodeBST = currNode;
        }
        if (!isBST) {
            maxNodeBST = LargestBST(currNode.getLeftPointer(), maxNodeBST);
            maxNodeBST = LargestBST(currNode.getRightPointer(), maxNodeBST);
        }
        return maxNodeBST;
    }

- Krishna August 10, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Krishna
no point in writing this code, given tree is not a BST.
Morons!!

- aravind646 November 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

i dont get it...any subtree of a bst will be a bst...coz to check IsBST() we check ltree , rtree are BST or not...so basically we just need to find which tree(left/right) of root has more nodes....correct if i am wrong

- utscool February 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

OK its not given that the given tree is a BST...sorry "mea culpa"!!

- Yoda February 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@utscool not your fault.
The statement says: In a given binary tree,find the largest subtree that's **also** a BST.
What does this **also** mean? :)

- becga March 11, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

there may be more than 1 subtrees which satisfy the bst property...we need to find the biggest tree from all such trees...

- utscool March 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A small doubt.. even if lcount>rcount , there are chances that the largest bst would be in root-rchild tree. Does this program takes care of this situation ?

- neo March 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

"Anonymous on February 02, 2010" looks not correct.

for function: int isBst(node *root)

if(!isBst(root->left)&&!isBst(root->right))

need change to

if(!isBst(root->left) || !isBst(root->right))

- philip April 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I got following sln, I think it is more neat. Basically the function return the BST size if the current sub tree is a BST, otherwise return -1. There are static parameters that save the max BST and its size.

class BTNode
{
    public int val;
    public BTNode left;
    public BTNode right;
}

static int GetMaxBstSize(BTNode Node, ref BTNode MaxBst, ref int MaxBstSize)
{
    int leftSize, rightSize, thisSize;
    if (Node == null)
        return 0;
    leftSize = GetMaxBstSize(Node.left, ref MaxBst, ref MaxBstSize);
    rightSize = GetMaxBstSize(Node.right, ref MaxBst, ref MaxBstSize);
    if (leftSize < 0 || rightSize < 0)
        return -1;
    if ((Node.left != null && Node.left.val > Node.val) ||
        (Node.right != null && Node.right.val < Node.val))
        return -1;
    thisSize = leftSize + rightSize + 1;
    if (MaxBstSize < thisSize)
    {
        MaxBstSize = thisSize;
        MaxBst = Node;
    }
    return thisSize;
}

- Anonymous July 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The Solution is to find the Largest BST In a given BT , let me know if there is some thing wrong with the following code

public Node LargestBST(Node currNode, Node maxNodeBST) {

        if (currNode == null) {
            return null;
        }
        boolean isBST = isBST(currNode);
        int size = size(currNode);
        int sizeOfMaxBst = size(maxNodeBST);
        if (isBST && sizeOfMaxBst < size) {
            maxNodeBST = currNode;
        }
        if (!isBST) {
            maxNodeBST = LargestBST(currNode.getLeftPointer(), maxNodeBST);
            maxNodeBST = LargestBST(currNode.getRightPointer(), maxNodeBST);
        }
        return maxNodeBST;
    }

- Anonymous August 10, 2010 | 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