VMWare Inc Interview Question for Member Technical Staffs


Country: India
Interview Type: In-Person




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

typedef struct tree {
   int val;
   struct tree *left;
   struct tree *right;
} tree_t;

void max_bt(tree_t *root, int *max_value)
{
   if (root == NULL) 
       return;

  max_bt(root->left, max_value);

  if (root->val > *max_value)
      *max_value = root->val;

  max_bt(root->right, max_value);

  return;
}

int main(int argc, char *argv[])
{
        int max_value = INT_MIN; /* Some minimum value which tree key values cannot assume */

        /* construct_tree not define, define it yourself */
        tree_t *root = construct_tree();
             
        max_bt(root, &max_value);

        printf("Max value from the given binary tree : %d\n", max_value);

	return 0;
}

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

Correct One... :)

- coding.arya May 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Do any tree traversal(inorder or preorder or postorder) and keep updating max element

rough algo
void maxelementoftree(struct node* root,int* countref)
{
if(root == NULL)

return;
else
{
maxelementoftree(root->left , countref);
if(root->data > *countref)
*countref = root->data;
maxelementoftree(root->right, countref);
}
}

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

typedef struct tree {
int val,
struct tree *left;
struct tree *right;
} tree_t;

tree_t * FindLargest(tree_t* node) {
    if (NULL == node) 
       return NULL;
    tree_t * left_largest = FindLargest(node->left);
    tree_t * right_largest = FindLargest(node->right);
    
    if (NULL == left_largest && NULL == right_largest) 
        return node;
    if (NULL != left_largest && NULL ! = right_largest) {
        if ((node->val > right_largest->val)  && (node->val > left_largest->val))  
            return node;
        else if (right_largest->val > node->val)  && (right_largest->val >   left_largest->val))
           return right_largest;
        else 
             return left_largest;
    }
     if (left->largest == NULL)
         return (node->val > right_largest->val ? node : right_largest);
     else
         return (node->val > left_largest->val ? node : right_largest);             
}

Basically I'm trying to compare the values of the parent , its left child and right child.

1) If no child is parent , the largets value is of its parent.
2)if both the children are present , return the largest value among the parent and the children
3)If either of the children is present , do the same as case 2

So from each subtree I'm getting the highest value recursively

- sumit.083 May 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int getmax(const node* root)
{
	if(root == NULL) return INT_MIN;

	int leftmax = getmax(root->left);
	int rightmax = getmax(root->right);

	return max(leftmax, max(root, rightmax));
}

- Anonymous August 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

typedef struct node{
        int data;
        struct node* left;
        struct node* right;
}tree_node;

int max(int a, int b, int c)
{
     int m = a;
     (m < b) && (m = b); //these are not conditional statements.
     (m < c) && (m = c); //these are just boolean expressions.
     return m;
}

int max_element(tree_node* root){
        if(root == NULL)
                return;
        if(root->left == NULL && root->right == NULL){
                //Hit leaf
                return root->data;
        }    
        return max(root->data, max_element(root->left),max_element(root->right));
}

- jdaemon February 13, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

traverse the tree in Inorder form so that you will get the node value in ascending order. Hence the last node value will be the highest value for the binary tree..

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

@Anonymous: tree is not BST, so inorder will not work.
Try level order traversal and assign temp variable with small value and keep searching bigger element.

- Razz May 06, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Let's assume the tree we're given is defined as follows:

class BinaryTree
{
    BinaryTree left, right; 
    int value;
}

Then, we can accomplish the task with a very simple recursive method. We take the max of the root and the overall max from each of the subtrees, and return the largest of those 3 values. When a node is NULL, we return Integer.MIN_VALUE so it will effectively be ignored in the calculation of the max (since it cannot be greater than any other value that is included in the max calculation, it will have no effect on the outcome). We assume here that the top-level root passed into the method is not null (since if it is, there's no well-defined answer).

public static int maxVal (BinaryTree root)
{
    if (root == null) { return Integer.MIN_VALUE; }
    int maxSoFar = Math.max(maxVal (root.left), maxVal(root.right));
    return Math.max (maxSoFar, root.val);
}

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

Python:
def maxval(root):
    if not root:
        return None
    return max(root.val, maxval(root.left), maxval(root.right))

- Cloudster May 06, 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