Interview Question


Country: United States




Comment hidden because of low score. Click to expand.
12
of 12 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.
11
of 11 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.
1
of 1 vote

1) Take inorder traversal of BS tree.
2)Check if it is sorted in ascending order.

- xyz August 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Inorder traversal is also a nice solution. I will try to explain the algorithm without using any auxiliary array.

Let prev = -INFINITY;

//function to check if tree is BST
boolean traverse(Node *n){
	if(node n is NULL) return true;
	else if node n is a leaf and prev <= n {
		prev = n;
		return true;
	}
	boolean flag = false, leftFlag = false, rightFlag = false;
	leftFlag = traverse(n.left);	//check left subtree
	if(prev <= n.key) {
		prev = n.key;
		flag = true;
	}
	rightFlag = traverse(n.right);	//check right subtree

	return (leftFlag && flag && rightFlag);
}

- EOF August 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Following is the pseudocode:

Triplet {
	boolean isBST;	//The tree is BST or not
	int min;	//Minimum value in the tree
	int max;	//Maximum value in the tree
}

//check if subtree rooted at "root" is a BST
Triplet checkBST(Node root) {
	Triplet leftT, rightT;

	//IGNORING BOUNDARY CASES LIKE LEAVES ...
	leftT = checkBST(root.left);
	rightT = checkBST(root.right);

	//if both left and right subtrees are BSTs
	if(leftT.isBST && rightT.isBST) {
		//if this node is following the BST property then
		if(leftT.max<=root.key && root.key<=rightT.min)
			//the subtree rooted at this node is BST
			return new Triplet(true, leftT.min, rightT.max);
	}
	return new Triplet(false, leftT.min, rightT.max);	//not a BST
}

- EOF August 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void check(sn *start){
sn * prev;
if(start != NULL){
prev = start;
check(start->left)
if(prev -> data > start -> data)
printf("The no = %d\n",start -> data);
else{
printf("It is not bst\n");
exit(0);
}
check(start -> right);
else
return;
}

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

Chk this code@@its working

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

Just apply the normal inorder traversal. As inorder gives us a sorted set of values so if at any time the given node's data is smaller than the previous node then the given node is not a bst

- vgeek August 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The following code is O(n). The concept is that if every node in the tree is a BST Node, then the tree is a BST. Basically if the node has a left or right child, you need to make sure
1) That the left child is smaller than it, and the right child is larger than it
2) that the left and right children are also binary search tree nodes

As soon as a condition doesn't apply you should return false. In the second if statement I could reduce some redundancy of only checking the left node if the right node was a BST node.

bool isBST(Node root)
{
	bool isBSNode = true;
	if (root == null)
		return isBSNode;
	if (root.Right != null)
	{
		if (root.Value >= root.Right.Value) 
			return false;
		isBSNode = isBSNode && isBST(root.Right);
	}
	if (root.Left != null)
	{
		if (root.Value <= root.Left.Value) 
			return false;
		isBSNode = isBSNode && isBST(root.Left);
	}
	return isBSNode;
}

- tipbruley August 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Actually I could save some complexity by doing the following

if (root.Right != null)
	{
		if (root.Value >= root.Right.Value) 
			return false;
		isBSNode = isBST(root.Right);
	}
	if (isBSNode && root.Left != null)
	{
		if (root.Value <= root.Left.Value) 
			return false;
		isBSNode =  isBST(root.Left);
	}

- tipbruley August 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Doing a preorder traversal of the tree would give a fail-fast result. It means stop traversing the tree as soon as a node is found not to comply with BST.

public boolean isBST(Node root) {
    if(root==null) return true;

    return (root.left==null || root.value >= root.left) // Check current node with left node
		&& (root.right==null || root.value <= root.right ) // Check current node with right node
		&& isBST(root.left) && isBST(root.right);
}

- edlai August 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Edlai, you really don't do any different logic than the solution above.

- tipbruley August 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

There is mistake in the solution. If root node is greater than or equal to left node and smaller than or equal to right node and both left node and right node it self is BST then also it is possible that it's not a valid bst. You need to add a condition and need to check it recursively that root node is always greater than or equal to Max(root-->left) and less than or equal to Min(root-->right)

here is the java implementation of this

public boolean isBST(BinaryNode node)
	{
		boolean isBst = true;
		if (node == null)
		{
			return true;
		}
		
		if(node.left != null)
		{
			isBst = (node.data >= node.left.data) && (node.data >= max(node.left).data) && (isBST(node.left));
		}
		
		if(isBst && node.right != null)
		{
			isBst = (node.data <= node.right.data) && (node.data <= min(node.right).data) && (isBST(node.right));
		}
		
		return isBst;

	}

- aviundefined September 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

private boolean isBSTAlternative(Node node, NodeHolder lastNode)
{
if (node == null)
return true;
if (isBSTAlternative(node.left, lastNode))
{
if (lastNode.node != null) {
if (lastNode.node.data > node.data)
return false;
}
lastNode.node = node;
return isBSTAlternative(node.right, lastNode);
}
return false;
}

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

bool checkBST(Node* p,int* depth)
{     
	if(p==NULL)
	{
		*depth=0;
		return true;
	}
	int left,right;
	if(checkBST(p->left,&left)&&checkBST(p->right,&right))
	{
		int temp=*left-*right;
		if(temp<-1||temp>1)
			return false;
		*depth=1+(*left>*right?*left:*right);
	}
	return true;
}

- fword August 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public boolean BST(Tree root){
boolean flag;
if(root.value){
return true;
} else if(root.getLeft().value<root.value && root.getRigth().value>root.value){
flag = BST (root.getLeft());
if(flag==true){
flag = BST(root.getRigth());
} else {
return flag;
}
} else {
return false;
}
return flag;
}

- DukeMalik August 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public boolean BST(Tree root){
		 boolean flag;
		if(root.value){
			return true;
		} else if(root.getLeft().value<root.value && root.getRigth().value>root.value){
			flag = BST (root.getLeft());
			if(flag==true){
				flag = BST(root.getRigth());
			} else {
				return flag;
			}
		} else {
			return false;			
		}
		return flag;
	}

- Dukemalik August 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

java:

public boolean isBST(TreeNode root){
return isBST(root, Integer.MIN_VALUE, Integer.MAX_VALUE);
}
isBST(TreeNode root, int min, int max){
if(root==null)
return true;
if(root.val>=min&&root.val<max){
return isBST(root.left, min, root.val)&&isBST(root.right, root.val, max);
}
else return false;
}

- genier August 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Correct solution is
1) root.data >= root-->left.data and root.data >= Max(root-->left) and root--left is BST
2) root.data <= root-->right.data and root.data <= Min(root-->right) and root-->right is BST

public boolean isBST(BinaryNode node)
	{
		boolean isBst = true;
		if (node == null)
		{
			return true;
		}
		
		if(node.left != null)
		{
			isBst = (node.data >= node.left.data) && (node.data >= max(node.left).data) && (isBST(node.left));
		}
		
		if(isBst && node.right != null)
		{
			isBst = (node.data <= node.right.data) && (node.data <= min(node.right).data) && (isBST(node.right));
		}
		
		return isBst;

	}
	
public BinaryNode min(BinaryNode node)
	{
		if(node == null)
		{
			return node;
		}
		if (node.left == null)
		{
			return node;
		}
		else
		{
			return min(node.left);
		}
	}

	public BinaryNode max(BinaryNode node)
	{
		if (node.right == null)
		{
			return node;
		}
		else
		{
			return max(node.right);
		}
	}

- aviundefined September 08, 2013 | 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