## 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;
}

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.

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;
}

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;
}

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;
}

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);
}
}

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.

### 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.