Directi Interview Question for Developer Program Engineers


Country: India
Interview Type: Written Test




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

Let us consider a different problem:

Find a path from the root to a leaf node (define such paths to be 'legs') which has the maximum sum.

Now this can easily be solved easy via a depth first search (or a recursive algorithm).

Now consider the problem we want to solve:

Find the path between two leaf nodes which has the maximum sum.

Now suppose that the path we seek passes through the root. This can now be composed of two(or less) legs (see definition earlier) corresponding to each subtree. Notice that both the legs must be the maximum for the corresponding subtree.

So we break down the problem as follows.

1) Try to recursively find the maximum leaf to leaf path in both subtrees (i.e. not passing through the root).

2) Find the maximum legs in each subtree and add up, and compare with what you got in 1.

Both 1 and 2 can be combined into one recursive solution.

- __NAME__ December 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

NAME is correct.. here is what it looks like:

int maxSumOfLeafpaths(node* root, int * maxSum){
    if (root==NULL)
        return -1;
    if (root->right==NULL && root->left ==NULL)
        return root->data; 
    int leftSum =  maxSumOfLeafpaths(root->left, maxSum);
    int rightSum =  maxSumOfLeafpaths(root->right, maxSum);
    int maxSumAtCurrLevel =  MAX(leftSum,rightSum)+ root->data;
    if (leftSum+rightSum+ root->data > *maxSum){
        *maxSum =  leftSum+rightSum+ root->data;
    }
    return maxSumAtCurrLevel;
}

- vik January 01, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why not use a modification of Dijkstra to find longest path considering the number in node as weight

- 123Nirvik August 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int maxLeafSum(TreeNode* root, int ps) // ps -> pathSum from root to left;
{
       if (root == NULL) return 0;
       
       int lps (0), rps(0);
       int lans = root->left ? maxLeafSum(root->left, lps) : INT_MIN;
       int rans = root->right ? maxLeafSum(root->right, rps) : INT_MIN;
       ps = t->left + max(lps, rps);
       return max(t->left + lps + rps, max(lans, rans));
}

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

sorry, it has to be

int maxLeafSum(TreeNode* root, int& ps)

- Anonymous July 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

// algo has some bugs fixed now
int maxLeafSum(TreeNode* root, int& root_to_leaf_sum)
{
if (root == NULL) return INT_MIN;
if (root->left == NULL && root->right == NULL) {
ps = root->val;
return INT_MIN;
}
int left_root_to_leaf_sum(0), right_root_to_leaf_sum(0);
int lans = maxLeafSum(root->left, left_root_to_leaf_sum);
int rans = maxLeafSum(root->right, right_root_to_leaf_sum);
root_to_leaf_sum = root->val + max(left_root_to_leaf_sum, right_root_to_leaf_sum);
return max(lans, rans, root_to_leaf_sum);
);
}

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

question is not very clear.can any one explain it?

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

it is same as finding the diameter of a tree.. isnt it??

- anamika July 30, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool hasPathSum(TreeNode* root, int sum, int ps)
{
     if (root == NULL) return false;
     ps += root->val;
     if (ps == sum) return true;
     else return hasPathSum(root->left, sum, ps) | hasPathSum(root->left, sum, ps);
}

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

Don't you think its a modification of diameter in a binary tree where you have to do the little modification, using the values of nodes.

Check this out:-
http: www geeksforgeeks.org diameter-of-a-binary-tree

where \ is replaced by space

Please share your thoughts

- varun October 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Right, if each node value is positive.

- node January 04, 2014 | 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