Directi Interview Question


Country: India
Interview Type: Phone Interview




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

bool IsBST(struct node * root) {
static struct node* prev = NULL;
if(root)
{
if(! IsBST(root->left)
return false;
if(prev &&(prev->data > root->data)
return false;
prev = root;
return IsBST(root->root);
}
return true;
}

- Anonymous November 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool IsBinarySearchTree(struct node * root)
{

if (NULL == root)
return true;

if (NULL != root->left && root->data < root->left->data)
return false;

if (NULL != root->right && root->data > root->right->data)
return false;

return IsBinarySearchTree(root->left) && IsBinarySearchTree(root->right);

}

- Vinod November 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static boolean isBST ( Node root ) {

		List<Integer> inOrderList = new ArrayList<Integer>();
		getInorder(root, inOrderList);

		int max = inOrderList.get(0);
		for ( Integer element : inOrderList ) {
			if ( element < max ) {
				return false;
			} else {
				max = element;
			}
		}
		
		return true;

	}

	private static void getInorder ( Node root, List<Integer> numberList ) {

		if ( root != null ) {
			getInorder(root.getLeft(), numberList);
			numberList.add(root.getVal());
			getInorder(root.getRight(), numberList);
		}
	}

- belligerentCoder November 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

space complexity is O(n), can't we do better?

- Andi November 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@eugene: Nitpick: "You can beat O(n)" is kind of nonsensical, because BigOh denotes an upper bound...

- Anonymous November 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

can = can't...

- Anonymous November 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I'm not talking about time complexity, I'm talking about space complexity.

- Andi November 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

guess your overlook at it

- Andi November 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Did you miss to check if node's value lies between lbvalue and rbvalue in the code?

- ACP Pradyuman November 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Did you miss to check if node's value lies between lbvalue and rbvalue in the code?

- ACP Pradyuman November 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This algorithm has O(n) space complexity.
We can have the following algorithm if we want to avoid space.

public static boolean isBSTWithNoSpace(Node root) {

		if ( root != null ) {
			int rootVal = root.getVal();
			Node leftNode = root.getLeft();
			Node rightNode = root.getRight();

			// Case where the node is a leaf node
			if ( leftNode == null && rightNode == null ) {
				return true;
			}

			// Case where the node has both right and left child
			if ( leftNode != null && rightNode != null ) {

				if ( leftNode.getVal() < rootVal && rightNode.getVal() > rootVal ) {
					return isBSTWithNoSpace(leftNode) && isBSTWithNoSpace(rightNode);
				} else {
					return false;
				}
			}

			// Case where the node has only the left child
			if ( leftNode != null ) {

				if ( leftNode.getVal() < rootVal ) {
					return isBSTWithNoSpace(leftNode); 
				} else {
					return false;
				}
			}

			// Case where the node has only the right child
			if ( rightNode != null ) {

				if ( rightNode.getVal() > rootVal ) {
					return isBSTWithNoSpace(rightNode); 
				} else {
					return false;
				}
			}
			
			return false;
		} else {
			return false;
		}

	}

Sorry, for the bad method name.

@ACP Pradyuman
I am first traversing the given tree in-order and then checking if the list of integers that I obtain is in increasing order.

Time complexity - O(n) (normal traversal)
Space complexity - O(n) (storing n elements)

Another bad thing about the algorithm is it cannot break in between. It would always traverse the whole tree. Making the best, average and worst time O(n)

On the other hand, the above algorithm doesn't bother to look further if a particular sub tree doesn't exhibit the BST property.

- belligerentCoder November 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Complete code with all inlined comments for both BST definition and algorithm:

struct Node {
    Node *left;
    Node *right;
    int value; 
};
/**
    Definition of a BST and algorithm.
    Given tree is considered a BST if: (1 || (2 && 3))
    1. It is an empty tree;
    2. All the nodes in the left subtree of any node have values less than
       or equal to the given node's value && is a BST itself;
    3. All the nodes in the right subtree of any node have values greater than
       the given node's value && is a BST itself;
      
    @param lbValue - the minimum allowed value of the given node
    @param ubValue - the maximum allowed value of the given node
    
    Any node should meet this constraint: lbValue <= node.value < ubValue
    
    The value range for any node is as follows:
      For any node in the left subtree of node N:
      lbValue <= child.value < N.value
      
      For any node in the right subtree of node N:
      N.value < child.value <= ubValue
*/
bool IsBST(Node *node, const int& lbValue, const int& ubValue)
{
    if (NULL == node)
        return true;
    
    /* Using a tail recursion here to eliminate the stack frame inflation */
    return ( (IsBST(node->left, lbValue, node->value)) && 
             (IsBST(node->right, node->value, ubValue)) );
}
int main ()
{
    Node *root = GenerateBinaryTree(...); // code to generate a BT (not necessarily a BST)
    bool bIsBST = IsBST(root, INT_MIN, INT_MAX);
    
    return 0;

}}

- ashot madatyan November 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

On what conditiom will isBst return false

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

Yes, @ashot, one vote from me.
It looks to be working good.
We can call the API IsBsT(root,INT_MIN,INT_MAX). The header <limits.h> provides us with this facility.

- codetheuniverse February 16, 2013 | Flag


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