Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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.
10
of 10 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.
4
of 4 vote

It is simple, you can do an in-order traversal of the tree and every time you read the root, put that to an array, then you can go through the array by comparing elements which should be in ascending order.

public void checkBST(Node tree){
if(tree!=null){
checkBST(tree.left);
arrayList.put(tree.data);
checkBST(tree.right);
}
}

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

I had given the same answer in MS interview.

- Sanjay Kumar December 24, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@sanjay is that good one ?

- aaron liang September 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

static boolean isBST(TreeNode root){
if(root == null)
return true;
if(root.left!=null && root.left.data > root.data ||
root.right!=null && root.right.data < root.data)
return false;
return(isBST(root.left) && isBST(root.right));

}

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

Your code will break down for the following tree:

7
     /  \
    3    11
   / \   /
  2   8 9

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

Thanks....
Here is the new one
static boolean isBST(TreeNode root){
return isBST(root,Integer.MIN_VALUE,Integer.MAX_VALUE);
}

static boolean isBST(TreeNode root,int min,int max){
if(root == null)
return true;
if(root.data<min || root.data>max)
return false;
if(root.left!=null && root.left.data > root.data ||
root.right!=null && root.right.data < root.data)
return false;
return(isBST(root.left) && isBST(root.right));
}

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

I think this one only can check if the tress is binary , not sure for bst

- aaron liang September 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

If you have a tree how to verify it a BST

static boolean isBST(Node root) {
if(root == null)
return false;
if(root.left != null && root.left.value < root.value) {
return true;
}
if(root.right != null && root.right.value > root.value) {
return true;
}

if((root.left != null && root.left.value > root.value)
|| (root.right != null && root.right.value > root.value)) {
return false;
}
isBST(root.left);
isBST(root.right);

return true;
}

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

better Solution will be

static boolean isBST(TreeNode root){
if(root == null)
return true;
if(root.left!=null && root.left.data > root.data ||
root.right!=null && root.right.data < root.data)
return false;
return(isBST(root.left) && isBST(root.right));

}

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

won't work...
50
25 75
12 55 60 90

- CrimePatrol December 25, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

static boolean isBST(TreeNode root){
return isBST(root,Integer.MIN_VALUE,Integer.MAX_VALUE);
}

static boolean isBST(TreeNode root,int min,int max){
if(root == null)
return true;
if(root.data<min || root.data>max)
return false;
if(root.left!=null && root.left.data > root.data ||
root.right!=null && root.right.data < root.data)
return false;
return(isBST(root.left,min,root.data) && isBST(root.right,root.data,max));
}

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

can you please tell me your algo.i don't think anyone doing here is correct for checking BST except inorder traversal and storing in array and then check it is sorted or not.
pls explain me your algo .
thanks

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

I'm not sure why everybody wanna make it so complex :)
The question was simple and my answer was straight forward and the Amazon interviewer agreed with me when I mentioned the inorder traversal solution. I guess there may be different solutions, but don't make it over complicated, you have only may be 10 mintues to discuss the algo and then code it correctly.

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

I am passing min and max value for every node... for eg if i go to left node, i will pass current node's value as a max for next node and so on.

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

check every node of tree from its parent node and grandparent nodes too.when do recursive then it would have been check that grandparent node from their grandparent node.finally some point it would be checking from root value.

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

int isThisABST(struct node* mynode)
{
if (mynode==NULL) return(true);
if (node->left!=NULL && maxValue(mynode->left) > mynode->data) return(false);
if (node->right!=NULL && minValue(mynode->right) <= mynode->data) return(false);
if (!isThisABST(node->left) || !isThisABST(node->right)) return(false);
return(true);
}

Reference nice material

- Anonymous January 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Use 2 stacks to create a queue structure
take 2 stacks...stack1 and stack2
now push the elements one by one into stack1 then pop them one by one and push them onto stack2.now on the whole push onto stack1 and pop fro stack 2,which is a queue,first in first out implemented.

- raghu December 23, 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