Microsoft Interview Question for Software Engineer / Developers






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.
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.
0
of 0 vote

bool IsBst(Node n) {
 if (n == null) return true;

 if (n.left != null && n.right != null) {
  if (n.val < n.left.val || n.val > n.right.val) return false;
 } else {
  if (n.left != null) {
   if (n.val < n.left.val) return false;
  } else {
     if (n.right != null) {
      if (n.val > n.right.val) return false;
     } else 
        return true;
  }
 }
 return IsBst(n.left) && IsBst(n.right);
}

O(n) - time & space (use of stack)

- S March 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

does not work, consider:

10
   6       12
 5   15  11  13

the subtree of 6-5-15 will return true, but clearly it violates the basis of a BST given 15 > 10.

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

Oh yes... :)...
Then, the in-order traversal should be ordered.

- S March 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Already discussed id=8186287

- Tulley March 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool isBST(Node * ptr)
{
	if (ptr==NULL) return TRUE;
	if (ptr->right == NULL && isBST(ptr->left) && ptr->value > ptr->left->value )return TRUE;
	if (ptr->left == NULL && isBST(ptr->right) && ptr->value < ptr->left->value )return TRUE;
	if(isBST(ptr->left)&& isBST(ptr->right) && ptr->value > ptr->left->value &&  ptr->value < ptr->left->value) return TRUE;
	return FALSE;

}

- Roy May 11, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool isBST(Node * ptr)
{
	if (ptr==NULL) return TRUE;
	if (ptr->right == NULL && isBST(ptr->left) && ptr->value > ptr->left->value )return TRUE;
	if (ptr->left == NULL && isBST(ptr->right) && ptr->value < ptr->right->value )return TRUE;
	if(isBST(ptr->left)&& isBST(ptr->right) && ptr->value > ptr->left->value &&  ptr->value < ptr->left->value) return TRUE;
	return FALSE;

}

- Roy May 11, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Roy that solution does not work. See the above post by "Anonymous" (it was me). It will fail that tree.

- woohoo May 11, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

please visit this site : code-forum.blogspot.com/2010/12/is-bst-or-not.html

- priyaranjan June 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool isValidBST(node *p, int minAllowedValue, int maxAllowedValue)
{
if(p == null)
return true;

if(p->value < minAllowedValue || p->value > maxAllowedValue)
return false;

return isValidBST(p->left, minAllowedValue, p->value)
&& isValidBST(p->right, p->value, maxAllowedValue);

}

- WellSaid June 30, 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