Ebay Interview Question for Software Engineer / Developers


Country: United States
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.
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.
2
of 2 vote

Java:

public boolean isBST(Node Root)
{
   if (Root == null)
      return true;
   if (Root.value < Root.left.value || Root.value >= Root.right.value)
      return false;
   else
      return isBST(Root.left) && isBST(Root.right)
}

- rajarshi129 October 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

If root.left is null it would fail on root.left.value!

- Sara March 05, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This does not work for this case
5
3 6
1 8
You alg will return true but this is not a BST. Your alg is wrong.

- cscs54cs March 08, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

void main()
{
isBst(root, Int.min, Int.max);
}

bool isBst(Node *r, int min, int max)
{
if(r == null) return false;

if(r->data < min || r -> data >= max) return false;

bool l = true, r = true;
if(r -> left != null)
l = isBst(r->left, min, r->data);

if(r->right != null)
r = isBst (r->right, r->data, max);


return (l & r);


}

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

if r is null, it should return true since empty node is a BST. And that's the only way this will succeed.

if(r == null) return true;

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

much simpler

isBST(root, MIN_INT, MAX_INT);

    bool isBST(Node *node, int min, int max){
        if( node == null)
            return true;

        if(node->value < min || node->value > max)
            return false;

        return isBST(node->left, min, node->value) && isBST(node->right, node->value, max);
    }

- dead.rabbit March 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/**
Tests if a tree meets the conditions to be a
binary search tree (BST). Uses the efficient
recursive helper.
*/
public boolean isBST2() {
return( isBST2(root, Integer.MIN_VALUE, Integer.MAX_VALUE) );
}
/**
Efficient BST helper -- Given a node, and min and max values,
recurs down the tree to verify that it is a BST, and that all
its nodes are within the min..max range. Works in O(n) time --
visits each node only once.
*/
private boolean isBST2(Node node, int min, int max) {
if (node==null) {
return(true);
}
else {
// left should be in range min...node.data
boolean leftOk = isBST2(node.left, min, node.data);

// if the left is not ok, bail out
if (!leftOk) return(false);

// right should be in range node.data+1..max
boolean rightOk = isBST2(node.right, node.data+1, max);

return(rightOk);
}
}

- ssso June 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

DFS traverse if you find left is not less then root or right is not greater then root then return false

- BS September 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You can just do a in-order traversal, and see whether the out put is in a increasing order

- cscs54cs March 08, 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