PayPal Interview Question for Member Technical Staffs


Country: India
Interview Type: In-Person




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

As we all know that we can use IN-Order traversing for this, without using any extra space.
But the implementation of this by Bob Dondero is simply awesome:

private boolean isBST() {
            return isBST(root, null, null);
    }
    private boolean isBST(Node x, Key min, Key max) {
        if (x == null) return true;
        if (min != null && x.key.compareTo(min) <= 0) return false;
        if (max != null && x.key.compareTo(max) >= 0) return false;
        return isBST(x.left, min, x.key) && isBST(x.right, x.key, max);
    }

Ref: leetcode.com/2010/09/determine-if-binary-tree-is-binary.html

- Ajeet October 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Ajeet, that's very nice.

- S O U N D W A V E October 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is a standard question, and that is the standard answer. Probably been there since before Boob dondero was born.

- Anonymous October 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Why would there be a standard answer for this? The way Ajeet coded it looked very clean.

But true on the second point, any algorithm to check the invariant of a basic data structure isn't worth a "invented by Dr. ______" label.

- S O U N D W A V E October 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Agreed. No need to invest further time on this if we can't gain anything in terms of space or time.

But a better implementation of classic algo is always needed.... :)

Actually we study these basic algos\DS since first day of our engineerig\science courses. So we use to these basic algorithms and we never think about second option to implement it.
I am using this algo (check BST) with same complexity since last three years but never think about this way (cleaner approach). We generally so much confident about basics that we dont think necessary to google it.
But if we use a complex\advanced data structure than we give it proper time to implement it. May be due to fear of complexity.

Just sharing my personal experience ....
3 years back I implemented BST with in half day (including all unit tests to verify boundaries etc).
But to implement a Suffix Tree take more than a week - excluding algo and complexity study ... :) I was already aware what algo i have to use ....I took this much time to try implementation with different approaches ... :)

- Ajeet October 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Elegant

- StrongTSQ October 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think the given input is tree and not for sure a binary tree. Hence, we have to check number of children for any node is not greater than 2

- mani 4m sklm May 03, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Let root node of the given tree be r
Do a in-order traversal of the tree; determine if it is bst in the process by comparing node's key with the previous one; if less, false, else continue until traversal is finished, then true;

pseudo codes in java:

ArrayList a; //store the nodes
Boolean result=true;//assume it is BST
void inOrder(Node n){
if(n.left != null){
inOrder(n.left);
}
push(n, a)
if(n.right != null){
inOrder(n.right)
}
}

void push(Node n, ArrayList a){
if(n.key <= a.last){
result = false;
through exception;
}else{
a.add(n.key);
}
}

main(){
try{
inOrder(r);
}catch(exception e){
false;
}
true;
}

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

public class Node {
    private Node left;
    private Node right;
    private int value;

    public Node(Node left, Node right, int value) {
        this.left = left;
        this.right = right;
        this.value = value;
    }

    public Node getLeft() {
        return left;
    }

    public Node getRight() {
        return right;
    }

    public int getValue() {
        return value;
    }

    public boolean isBinarySearch(){
        if(getLeft() != null && getRight() != null && getLeft().getValue() <= value && getRight().getValue() >= value) {
            return true;
        }
        return false;
    }

    public static void main(String[] args){
        Node eight = new Node(null, null, 8);
        Node twelve = new Node(null, null, 12);
        Node ten = new Node(eight, twelve, 10);
        System.out.println(ten.isBinarySearch());
    }
}

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

:)
Not good boss.
Too many issues to comment.
#1 advice: Look at how to design Java programs and design well encapsulated DSs/ADTs in Java.

- S O U N D W A V E October 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

With such a definition of tree node:

struct BTreeNode
{
        BTreeNode *left;
        BTreeNode *right;
        int value;
};

Here is solution:

bool isBinarySearchTree(BTreeNode *node)
{
    if (node) {
        bool leftOK = node->left ? (node->left->value < node->value) && isBinarySearchTree(node->left) : true;    
        bool rightOK = node->right ? (node->right->value > node->value) && isBinarySearchTree(node->right) : true;
        if (!leftOK || !rightOK) return false;        
    }

    return true;
}

- Krzysztof.G October 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You sure that works?

- S O U N D W A V E October 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, I have checked that on many cases. Can you find any that proves it does not work?

- Krzysztof.G October 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

First basic thing ... it will not work for a BST with duplicate values

- Ajeet October 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Definition of BST is to contain *only unique values* in each node:

From wikipedia:

In computer science, a binary search tree (BST), sometimes also called an ordered or sorted binary tree, is a node-based binary tree data structure which has the following properties:

- The left subtree of a node contains only nodes with keys less than the node's key.
- The right subtree of a node contains only nodes with keys greater than the node's key.
- The left and right subtree each must also be a binary search tree.
- There must be no duplicate nodes.

- Krzysztof.G October 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

In business use cases it is a common scenario. No idea about the def at wiki ..

- Ajeet October 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Assume that this is the tree:

		8
	       /  \
              6  10
	      /\    /\
             4 9 7 11

Code says this is BST, but it is not.

- Anonymous October 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Excuse me? The common business (and only) scenario is to store unique values in each node. This is how all stuff works (sets, maps/dictionaries).
If you need store more same values (whatever value is) you put in each node structure container to store all these values.
If you would like to support same values in tree (it would not be a BST!) then in this case:

10
/ \
5 15
/ \
3 7


If you would like to put '5' one more time in the tree you would need to re-build all tree below existing 5 number. What business case requires such ineffective and slow way of storing things?

- Krzysztof.G October 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Krzysztof, let's calm down.

Your diagram above itself immediately gives the idea of how to insert duplicates just as easily as throwing them out or overwriting.

Just take the subtree rooted at 3, put it aside.
Put the new duplicate 5 where the 3 was.
Make the old subtree, the new left subtree of the new 5.
It is O(1) extra work over the regular insert and doesn't require you to "rebuild" all the tree below 5.

It seems (picture a tree), that everything works smoothly if we allow duplicates to to go on one side (like left).
That way when you search works fine still in lg(height).

And duplicates can be designed into most DSs and they are useful (Duplicate numbers in real data, same names "John Smith", etc. etc.).

Regular BST stuff seems perfectly easy to convert to add handling of duplicates.

For balanced BSTs, we could probably convert the rebalancing stuff for this to on our own. Or we google it. Wikipedia isn't the ultimate authority on everything possible and probable.

Now take you diagram above, change the 7 to a 11.
1) Is it still a BST?
2) What will your BSTcheck do for that test case?

Take care

- S O U N D W A V E October 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi,

Well, I think anyway that common definition of whatever (in this case BST) is more than needed. In other case we can both be right even if we have completely opposit opinions on topic. Especially in computer science these definitions of things is our language. If it is not common there will be problems with understand each other.
To be consistent (at least in C++) it would be misleading to say that "set" can have non-unique values -> It is multiset. Every definition I found - and wikipedia seems to be right here - says that BST has only unique values.
(including Steven Skiena's "The Algorithm Design Manual"). If we do not follow definitions somebody can say you "Hey, your's solution does not allow duplicate values. Your solution is wrong!".
And of course algorithms written for BST will mostly work in BST-like (with duplicates) trees. But it is no more pure BST (at least for me ;-) ). No offense - just wanted clarify my point of view.

Thanks for finding bugs in my previous solution. Here is updated one (going from down to up of a tree):

bool isBinarySearchTree2(BTreeNode *node, int &min, int&max)
{
    if (node) 
    {   
        int leftMin, leftMax, rightMin, rightMax;

        bool leftOK  = node->left ? 
                       isBinarySearchTree2(node->left, leftMin, leftMax)  && (min = leftMin, node->value > leftMax) : (min = node->value, true);
        bool rightOK = node->right ? 
                       isBinarySearchTree2(node->right, rightMin, rightMax) && (max = rightMax, node->value < rightMin) : (max = node->value, true);

        if (!leftOK || !rightOK) return false;
    }

    return true;
}

BTW why somebody gave me minus for providing definition of BST from wikipedia... "somebody" - shame on you ! :-)

- Krzysztof.G October 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

These points are dumb.
Can't even trade it in for free McDonalds burgers.
Going to create a new account just to get back down to 0.

+1 for good debate.

- S O U N D W A V E October 26, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@ Krz,
I agree, anything can be anything.
Definitions are labels.
We need to move away from definitions and relying on them to write 6 word questions thus leading to multi-paragraph debates.

But yeah, if some random dude asked "What is a blah blah for BST?" I'd assume he meant the most basic form.

- S O U N D W A V E October 26, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

get the in-order visit sequence of the given tree, and check whether it is in order or not.

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

private static void checkBST(Node root) {
		if(root .left!= null) {
			if(root.value > root.left.value) {
				checkBST(root.left);
			} else {
				System.out.println("Its not a BST");
			}
		} if(root .right!= null) {
			 if(root.value < root.right.value) {
				checkBST(root.right);
			 } else {
					System.out.println("Its not a BST");
			}
		}

}

- Francis February 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int isBST(Node *node)
{
	if (node == NULL) return true;
	if (node->left != NULL) {
		if (node->left->Value > node->Value) return false;
	}
	if (node->right != NULL) {
		if (node->right->Value < node->Value) return false;
	}
	
	return isBST(node->left) && isBST(node->right);	
}

- Nitin Gupta May 06, 2015 | 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