Amazon Interview Question for SDE-2s


Country: United States
Interview Type: In-Person




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

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 10, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Do an in-order traversal and see if the elements are sorted. O(n)

- mohd.husn001 September 23, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yeah agreed. Cant think of anything better than O(n). Am surprised that Amazon would ask such basic question.

- StrategicCoderForce September 23, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You cannot do it better than O(n) since you have to check each elements in the tree.

- ravio September 23, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

it becomes a little more interesting if you need to do it in O(0) additional space.

For that you need to bound the possible values as your traverse the tree:

bool isBST(node* n, int min=MIN_INT, int max=MAX_INT)
	{
		if(n==NULL)
			return true;
		
		if(n->value <= min && n->value >= max)
			return false;
		
		return isBST(n->left,min,n->value) && isBST(n->right, n->value, max);
	}

- Anonymous September 26, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

public class IsTreeBST {
	
	
	public static void main(String[] args) {
		
		NodeIsTreeBST temp = new NodeIsTreeBST();
		temp = temp.NodeIsTreeBST(6);
		temp.left = temp.NodeIsTreeBST(5);
		temp.left.left = temp.NodeIsTreeBST(3);
		temp.left.left.right = temp.NodeIsTreeBST(20);
		temp.right = temp.NodeIsTreeBST(33);
		temp.right.left = temp.NodeIsTreeBST(20);
		temp.right.right = temp.NodeIsTreeBST(50);
		
		
		System.out.println(isBST(temp, Integer.MIN_VALUE, Integer.MAX_VALUE));
	}
	
	
	public static boolean isBST(NodeIsTreeBST node , int min , int max) {
		
		if(node == null)
			return true;
		
		if(node.data < min || node.data>max)
			return false;
		
		
		if(!isBST(node.left, min, node.data-1) || !isBST(node.right, node.data+1, max))
			return false;
		
		
		
		return true;
		
	}

}


class NodeIsTreeBST {
	
	int data;
	NodeIsTreeBST left;
	NodeIsTreeBST right;
	
	public NodeIsTreeBST NodeIsTreeBST(int data) {
		
		NodeIsTreeBST temp = new NodeIsTreeBST();
		temp.data = data;
		temp.left = null;
		temp.right = null;
		return temp;
		
		
	}
}

- shukad333 September 23, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

If there is any element in the tree with Integer.MIN_VALUE or Integer.MAX_VALUE how does the above approach behave

- anonymous September 24, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int testBst(struct tree * root)
{
if (NULL == root)
return 0;
else
{
if (!( (!root->left || root->left->key < root->key) && (!root->right || root->right->key > root->key ) ))
return 0;
testBst(root->left);
testBst(root->right);
}
return 1;
}

- tauqir0007 September 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think this will work on O(logn)

- tauqir0007 September 24, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

it is impossible to solve this problem in less than O(n) since we must touch every node at least once.

- Anonymous September 25, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

It's faster if you do it without recursion. The big O(n) remains the same, however. So does the memory of O(log n):

public boolean isBst(TreeNode root){
  if(root == null){
    return false;
  }
  Stack<TreeNode> workingSpace = new Stack<TreeNode>();
  workingSpace.push(root);
  while(!workingSpace.isEmpty(){
    TreeNode node = workingSpace.pop();
    if(node.left != null){
      if(node.left.value > node.value){
        return false;
      }
      workingSpace.push(node.left);
    }
    if(node.right != null){
      if(node.right.value <= node.value){
        return false;
      }
      workingSpace.push(node.right);
    }
  }
  return true;
}

- zortlord October 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

For success scenario, it is o(n), but in case if the given tree in not BST like either its left or right child branch has more number of levels, then we need to break the iteration and we can mention that this is not BST.
This can be done by calculating the level of the node by inorder, and once we reached the last leaf node, we can start calculating the total number of nodes that we are traversing from bottom to top, at any stage if it is against 2N -1 , then it is not BST

- nathgopi45 October 28, 2014 | 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