Microsoft Interview Question for Software Developers


Country: United States
Interview Type: In-Person




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

int getMax(TreeNode root, TreeNode* currentMax, enum attribValue)
{
    if(root == null)
        return 0;
    int count == 0;
    if(root.val == attribValue)
    {
        if(currentMax == NULL)
            currentMax = root;
        count++;
    }
    
    TreeNode* max = currentMax;
    if(root->left != NULL)
    {
        TreeNode* maxLeft = currentMax;
        int leftCount = getMax(root->left, maxLeft, attribValue);
        if(currentMax == maxLeft)
            count = count + leftCount;
        else
        {
            if(leftCount > count)
                currentMax = maxLeft;
            count = max(count, leftCount);
        }
    }
    
    if(root->right != NULL)
    {
        TreeNode* maxRight = max;
        int rightCount = getMax(root->right, maxRight, attribValue);
        if(currentMax == maxRight)
            count = count + rightCount;
        else
        {
            if(rightCount > count)
                currentMax = maxRight;
            count = max(count, rightCount);
        }
    }
    return count;
}

- Debatri April 01, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi hprem991, thanks a lot for providing the solution. I have one question, . How currentMax is being updated to point to the root of node that occurs most number of times, in this case 'R'. Could you explain or provide implementation if possible? Thanks a lot.

- smartbobby2K February 20, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int getMax(TreeNode root, TreeNode* currentMax, enum attribValue)
{
if(root == null)
return 0;
int count == 0;
if(root.val == attribValue)
{
if(currentMax == NULL)
currentMax = root;
count++;
}

TreeNode* max = currentMax;
if(root->left != NULL)
{
TreeNode* maxLeft = currentMax;
int leftCount = getMax(root->left, maxLeft, attribValue);
if(currentMax == maxLeft)
count = count + leftCount;
else
{
if(leftCount > count)
currentMax = maxLeft;
count = max(count, leftCount);
}
}

if(root->right != NULL)
{
TreeNode* maxRight = max;
int rightCount = getMax(root->right, maxRight, attribValue);
if(currentMax == maxRight)
count = count + rightCount;
else
{
if(rightCount > count)
currentMax = maxRight;
count = max(count, rightCount);
}
}
return count;
}

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

int getMax(TreeNode root, TreeNode* currentMax, enum attribValue)
{
    if(root == null)
        return 0;
    int count == 0;
    if(root.val == attribValue)
    {
        if(currentMax == NULL)
            currentMax = root;
        count++;
    }
    
    TreeNode* max = currentMax;
    if(root->left != NULL)
    {
        TreeNode* maxLeft = currentMax;
        int leftCount = getMax(root->left, maxLeft, attribValue);
        if(currentMax == maxLeft)
            count = count + leftCount;
        else
        {
            if(leftCount > count)
                currentMax = maxLeft;
            count = max(count, leftCount);
        }
    }
    
    if(root->right != NULL)
    {
        TreeNode* maxRight = max;
        int rightCount = getMax(root->right, maxRight, attribValue);
        if(currentMax == maxRight)
            count = count + rightCount;
        else
        {
            if(rightCount > count)
                currentMax = maxRight;
            count = max(count, rightCount);
        }
    }
    return count;
}

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

int getMax(TreeNode root, TreeNode* currentMax, enum attribValue)
{
    if(root == null)
        return 0;
    int count == 0;
    if(root.val == attribValue)
    {
        if(currentMax == NULL)
            currentMax = root;
        count++;
    }
    
    TreeNode* max = currentMax;
    if(root->left != NULL)
    {
        TreeNode* maxLeft = currentMax;
        int leftCount = getMax(root->left, maxLeft, attribValue);
        if(currentMax == maxLeft)
            count = count + leftCount;
        else
        {
            if(leftCount > count)
                currentMax = maxLeft;
            count = max(count, leftCount);
        }
    }
    
    if(root->right != NULL)
    {
        TreeNode* maxRight = max;
        int rightCount = getMax(root->right, maxRight, attribValue);
        if(currentMax == maxRight)
            count = count + rightCount;
        else
        {
            if(rightCount > count)
                currentMax = maxRight;
            count = max(count, rightCount);
        }
    }
    return count;
}

- Debatri April 01, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

TreeNode getMax(TreeNode root, TreeNode currentMax, enum attribValue){
if(root == null)
return currentMax;
if ((root->left.val == attribValue) && (root->left.val == attribValue)) {
getMax(root->left, currentMax, attribValue);
getMax(root->right, currentMax, attribValue);
} else {
getMax(root->left, root->left, attribValue);
getMax(root->right, root->right,, attribValue);
}
}

- hprem991 February 19, 2018 | 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