Amazon Interview Question for Software Engineer / Developers






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

int FindMax(Tree *root)
{
//Assuming root is not null.
if(root==NULL)
return 9999;
while(root->right)
root=root->right;
return root->element;
}

int FindMin(Tree *root)
{
//Assuming root is not null.
if(root==NULL)
return -9999;
while(root->left)
root=root->left;
return root->element;
}

bool isBST(Tree *root)
{
if(root==NULL)
return true;
else if(((root->left!=NULL) && (FindMax(root->left)>root->element))||
((root->right!=NULL) && (FindMin(root->right)<root->element)))
return false;
else
return (isBST(root->right) && isBST(root->left));
}


The idea is to compare root's element with the maximum element in the left subtree and root's element with the minimum element in the right subtree

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

bool isBST(TreeNode * root, int * pMin=NULL, int * pMax=NULL)
{
   if (!root) return true;
   if (pMin && root->data <= *pMin) return false;
   if (pMax && root->data > *pMax) return false;
   return isBST(root->left, pMin, &root->data) &&
          isBST(root->right, &root->data, pMax);
}

- fiddler.g July 31, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

inorder traversal should be sorted

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

IsBST(Tree *node)
{
    return IsBST(node, -infinity, infinity);
}
  
IsBinarySearchTree(Tree *node, int lower, int higher)
{
    if (node == null) return true;
    
    if (node->value < lower || node->value > higher) return false; 
  
    return IsBinarySearchTree(node->left, lower, node->value) && IsBinarySearchTree(node->right, node->value, higher); 
}

- Dabboo August 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes this is the right answer! Same question was asked from me. It was then further extended to find the subtree with maximum number of elements (the given tree need not to be a BST).

Solution of extended part: return integer indicating the number of elements present in the subtree(BST). A negative value will indicate that the subtree contains another subtree (not including itself) of intensity |integer|. If both left and right node returns +ve integers add them and then add 1(including this node) and return a +ve if the node itself follows BST and -ve if the node is not a part of BST.

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

If your extension question was 'finding the largest subtree which is a BST'...

Take inorder of the given tree and find the longest increasing contiguous sequence.

- Mahesh October 17, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry, doesn't work.

- Mahesh October 17, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Boolean IsBST(Node curNode)
{
static int lastValue = MIN_INT;
if (curNode == null)
return true;
if (!IsBST(curNode->left)
return false;
if (curNode->value < lastValue)
return false;
lastValue = curNode->value;
return IsBST(curNode->right);
}

- Anonymous August 04, 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