VMWare Inc Interview Question for Member Technical Staffs


Country: India
Interview Type: In-Person




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;
	}
TC - O(n2)
SC - O(n)

- 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);
	}
TC - O(n)
SC - O(n)

- Vir Pratap Uttam May 05, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
3
of 3 vote

Do inorder traversal, keep checking previously visited node is less then current node or not.
Please note, inorder traversal of binary tree prints data in sorted order.
Time Complexity: O(N), Space Complexity: O(N) for creating stack in recursive call.

- Razz May 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Providing code for the above approach

int prev = Integer.MIN_VALUE;
int current = 0;
boolean isValid = true;

public boolean validateBST (Node root){
	isValidBST (root);
	return isValid;
}

private boolean isValidBST (Node root) {

	if (root == null) {return;}

	isValidBST (root->left);

	if (isValid){
		current = root->value;
		if (current > prev) {
			prev = current;
		}else{
			isValid = false;
		}
	}

	isValidBST (root->right);
}

- arun_lisieux May 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Space complexity is O(h), where 'h' is the tree heigth.

- krusty June 06, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Complexity O(n):
Code:

BinaryTreeNode prev=null;
	public boolean isBST(BinaryTreeNode root){
		if(root!=null){
			if(!isBST(root.left))
				return false;
			if(prev!=null && prev.data>root.data)
				return false;
			prev = root;
			return isBST(root.right);
		}
		return true;
	}

- Dhass May 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool isBST(struct node* root)
{
      if(root==NULL)
      return true;

     if(root->data < min || root->data > max)      //min & max is minimum & maximum element.
     return false;

     else
     return isBST(root->lc,min,root->data) && isBST(root->rc,root->data+1,max)
}

- puneet kumar May 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

struct min_max{
    int min;
    int max;
    int isBst;
}
typedef min_max MN

bool validateBst(Node *head)
{
    MN * ans = isBst(head);
    boo a =  ans->isBst;
    free(ans);
    return a;
}

MN * isBst(Node *head)
{
    if(head == NULL)
        return NULL;
      
    MN* left_mn = isBst(head->left);
    MN* right_mn = isBst(head->right);
    
    MN * ans = (MN *)malloc(size(MN));
    
    if(left_mn->isBst == 0 || right_mn->isBst == 0){
        ans->isBst = 0;
        free(left_mn);
        free(right_mn);
        return ans;
    }
    
    ans->min = left_mn==NULL? head->val: left_mn->min;
    ans->max = right_mn==NULL? head->val: right_mn->max;
    
    if(ans->isBst == 1 && left_mn->max <= head->val &&  head->val < right_mn->min )
        ans->isBst = 1;
    else
        ans->isBst = 0;
    
    free(left_mn);
    free(right_mn);
    return ans
}

- jigs May 12, 2013 | 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