Amazon Interview Question for Developer Program Engineers


Team: Love Film
Country: UK
Interview Type: Phone Interview




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

/* check for BST */
	private static boolean isBST(BSTNode root) {
		if (root == null)
			return true;

		if (root.getLeft() != null && findMax(root.getLeft()) > root.getData())
			return false;
		if (root.getRight() != null
				&& findMin(root.getRight()) < root.getData())
			return false;

		if (!(isBST(root.getLeft())) || (!isBST(root.getRight())))
			return false;
		return true;
	}

- Vir Pratap Uttam May 05, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
11
of 11 vote

/* Check for binary tree whether its binary search tree */
	private static boolean isBST(BSTNode root, int prev) {
		if(root==null)
			return true;
		if(!isBST(root.getLeft(),prev))
			return false;
		if(root.getData()<prev)
			return false;
		prev=root.getData();
		
		return isBST(root.getRight(),prev);
	}

- Vir Pratap Uttam May 05, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
6
of 8 vote

The inorder traversal of the tree should be sorted.

- alex December 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
3
of 3 vote

BOOL IsBST(Node *Root)
{
    If(NULL == Root) return TRUE;
    int min = findMin(Root);
    int max = findMax(Root);
    return IsBST(Root, min-1, max+1);
}
BOOL IsBST(Node *Root, int MIN, int MAX)
{
    If(NULL == Root) return TRUE;
    return ( (Root->info > MIN) && 
             (Root->info < MAX) && 
	     IsBST(Root->Left, MIN, Root->Info+1) && 
	     IsBST(Root->Right, Root->Info-1, MAX) );
}

- SRB December 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think this : "
IsBST(Root->Left, MIN, Root->Info+1) &&
IsBST(Root->Right, Root->Info-1, MAX) );"
Should be :

IsBST(Root->Left, MIN, Root->Info) &&
IsBST(Root->Right, Root->Info, MAX) );
Because BST doesn't allow for the nodes to have the same value.

What do you think?

- Kamy December 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Its right.!

- Suhas March 14, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

bool isBST(struct node *node)
{
    return( isBSTHelper(node, INT_MIN, INT_MAX));
}
bool isBSTHelper(struct node *node, int min, int max)
{
  if( node == NULL)  
      return true;
  if( node->data < min || node->data > max )     
      return false;
  return ( isBST(node->left, min, node->data) &&
               isBST(node->right, node->data, max));
}

- code_freak December 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

boolean isBst(Node root){
if (root == null) return true;
else{
bool temp;
if(node->left != null && node->left->value < node->value)
temp = true;
if(node->right != null && node->right->value > node->value)
temp = true;
if(temp)
return isBst(node->left) && isBst(node->right) ;
else
return false;
}
}

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

/**correction**/
boolean isBst(Node root){
if (root == null) return true;
else{
bool temp = true;
if(node->left != null && node->left->value > node->value)
temp = false;
if(node->right != null && node->right->value < node->value)
temp = false;
if(temp)
return isBst(node->left) && isBst(node->right) ;
else
return false;
}
}

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

@anonymous: this is wrong since you are not comparing with the value of ancestor. Checking only with parent is not enough. Eg:
6
4 9
3 7

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

This algorithm does not work because you are not checking whether entire left-sub tree is lesser than the 'node->value' and entire right subtree is greater than the 'node->value'.

- CodePredator December 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

True, this doesn't work. Thanks for pointing it out.

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

bool flag  = true;
int max_encountered = NEGATIVE_INFINITY;
void checkInorder(Node root){
  if(root == null) return;
  if(!flag){
    println("Not a BST");
  }
  if(  flag && root.left != null)  
    checkInorder(root.left);
  if( flag && root.data > max_encountered){
    max_encountered = root.data
  }else{
    flag = false;
  }
   if( flag && root.right != null)  
    checkInorder(root.right);
}

- ks December 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public Boolean IsBst(Node node) {

- Amit December 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Please disregard my previous comment. Following is the solution as I thought:

public Boolean IsBst(Node nd) {
return IsBstI(nd, int.MinVal, int.MaxVal;
}

public Boolean IsBstI(Node nd, int min, int max) {
if (node.left != null) {
if (node.left < min || !IsBst(node.left, min, node.val) {
return false;
}
}

if (node.right != null) {
if (node.right > max || !IsBst(node.right, node.val, max) {
return false;
}
}

return true;
}

- Amit December 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Suppose the LEFTnodes from top to bottom are 10, 6, 4, 5
root
/
10
/
6
/
4
/
5

what will be the answer?

- Kary December 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

IsValidBST(root,Integer.MIN_VALUE,Integer.MAX_VALUE);

bool IsValidBST(BinaryNode node, int MIN, int MAX)
{
if(node == null)
return true;
if(node.element > MIN
&& node.element < MAX
&& IsValidBST(node.left,MIN,node.element)
&& IsValidBST(node.right,node.element,MAX))
return true;
else
return false;
}

- Minnu December 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

boolean isBst(Node n, int min, int max) {
if(n == null) return true;
if( n.data <= max || n.data > min) return false;
else return ( isBst(n.left, min, n.data) |&&
isBst(n.right, n.data, max));
}

first call will be isBst(root, -sentinel, sentinel); // sentinel = largest possible value.

- Zambola December 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If inorder traversal is in sorted order, then the tree is BST. Am I missing any other scenarios to consider ?

- Srikanth January 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

bool isBST(node* root)
{
bool isBst = true;
if (NULL == root)
return true;
if (root != NULL)
{
if (root->lchild != NULL && ((root->data) < (root->lchild->data))) {
isBst = false;
}
if (root->rchild != NULL && ((root->data) > (root->rchild->data))) {
isBst = false;
}
isBST(root->lchild);
isBST(root->rchild);
}
return isBst;
}

- Siddhartha December 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Its wrong, as you are not considering the cases when left subtree may have a right child which is bigger than right subtree node. e.g.
40
20 60
0 80

- Zambola December 24, 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