Adobe Interview Question






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

the checkbst returns 1 if tree is BST and return zero otherwise

int checkbst(struct node*node,int min,int max)//initial call is with min=INT_MIN and // max=INT_MAX
{
if (node==NULL) return 1;
if(node->data < min ||node->data>max) return 0;
return(checkbst(node->left,min,node->data && checkbst(node->right,node->data,max);
}

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

do in-order traversal and check if the sequence is increasing

- abc August 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Seems to be a correct answer, as all the below algo will fails when the right child of node X(which is a left child of its parent) is greater than X , but also greater than parent of x then the BST property of the tree gets voilated but the below stated also will pass.
Thus inorder is the best algo to find the BST .

- aalok October 12, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

this is very interesting observation! thank you, all dudes!

- spiderman July 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int isBST(struct node *ele)
{
if(node==NULL) return 1;
else if(node->left->value > node->value || node->right->value < node->value) return 0;
else return(isBST(node->left) && isBST(node->right))
}

- Pratik kathalkar August 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int isBST(struct node *ele)
{
if(node==NULL) return 1;
else if(node->left->value > node->value || node->right->value < node->value) return 0;
else return(isBST(node->left) && isBST(node->right))
}

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

int isBST(struct node *ele)
{
if(node==NULL) return 1;
else if(node->left->value > node->value || node->right->value < node->value) return 0;
else if ((node->left == NULL) && (node->right == NULL)) return 1;
else return(isBST(node->left) && isBST(node->right))
}

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

The codes above would crash if node->left is NULL when you access node->left->value.
also it doesn't consider the case where the BST is not complete.

A more complete version is

bool isBST(bnode * root)
{
if(root == NULL)
return true;
else if((root->left == NULL)||(root->left == NULL))
{
if((root->left == NULL)&&(root->left == NULL))
return true;
else if((root->left==NULL)&&(root->id >= root->right->id))
return false;
else if ((root->right==NULL)&&(root->id < root->left->id))
return false;
}
else if((root->id < root->left->id) ||(root->id >= root->right->id))
return false;
else
return (isBST(root->left) && isBST(root->right));
}

- Anonymous September 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

R u drunk ??
Please don't drink and derive...

- Bewda November 15, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Above algos are not correct, by just checking left and right child we can not be sure that it is a BST, we have to ensure that all the elements on right are bigger then the current node and all the elements on the left are smaller then the current one.

We can do this by passing minimum and maximum values while using recursion, search on google you will find the solution.

- avi November 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I liked the comment of bewda...

i think recursive solution is far better

- haha January 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a better solution:

bool isthisBST(nodeptr root)
{
return isthisBSTUtil(root,INT_MIN, INT_MAX);
}

int isthisBSTUtil(nodeptr root, int min, int max)
{
if(root == NULL) return true; //empty tree is a bst
if(root-data < min || root->data >max) return false;

return (isthisBSTUtil(root->left, min, root->data) || isthis BSTUtil(root->right,root->data, max);
}

NOte: we call isthisBST(root,INT_MIN,INT_MAX);

- fabregas March 04, 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