Directi Interview Question for Software Engineer / Developers






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

maxweight(currNode)
{
if (currNode.weight > maxWeight(left)+maxWeight(right))
return currNode.weight;
else
return maxWeight(left)+maxWeight(right);

}

- mrvishalg@rediffmail.com January 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

the solu is correct, just modified a little

maxweight(currNode)
{
if (currNode== NULL)
return 0; // provided no negative weights are given in the tree
if (currNode.weight > maxWeight(left)+maxWeight(right))
return currNode.weight;
else
return maxWeight(left)+maxWeight(right);

}.

- aravind646 November 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Shouldnt the code be

int calcmax(node *a)
{
if(root==NULL)
return 0;
if(root->left==NULL && root->right==NULL)
return root;

int x=root->val+max(root->left->left)+max(root->left->right)+max(root->right->left)+max(root->right->right);
int y=max(root->left)+max(root->right);
if(x>=y)
return x;
else
return y;
}

- Nitin Garg January 04, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Nitin I got the same logic and i hope this is correct. Just replace max by calcmax in ur code, it is typing error i guess.

- sachin April 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@nitin Perfect !!!

- CookieRaider July 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Its not clear can you explain it lil bit

- alok January 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

??? Can you please elaborate the question

- Anonymous January 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

What is the problem?

You have to output the set of nodes with the maximum weight. THe set has the constraint that if a node is in the set, its children aren't there in the set.


For eg:

1
   / \
  2   3
 /\    \
4  5    6
         \
          100
         

The set is {3,100,4,5}

- Anonymous January 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 votes

the solution to this would be only {3,100}.
I hope this clears the question a lot

- game January 12, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@ game.

What are you talking about?

- Anonymous January 12, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The correct answer for this question is [100, 4, 5]

- dr.house November 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

are sure its just the children of a node that should not be there? i mean can grandchildren of the node still be there?
i was confused by the line "if a node is in the subset, NONE of its children can be in that subset."

- Anonymous October 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

the example stated above must hav ( 5, 100) as the answer according to the conditions given

- anom July 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

use DP in TREE
maintain two values at each node dp[node][0],dp[node][1]
dp[node][0] maximum value that can be achieved without using this node
dp[node][1] maximum valus that can be achieved using this node.
try to compute the values of parent using the values of children.

- Atul Verma IIITA July 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

almost same as vertex cover problem...

- jackass October 27, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I was thinking of the same approach. Is this approach completely correct?

- whitesace December 15, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

if (p == NULL) return 0;
MaxTillNode p = max (MaxTill (children with out using the chilreen) + p->data, MaxTill(with using the children))

- krishna July 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry the above solution do not work since i missed some cases.

- krishan July 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*

A binary tree is given with a positive weight attached to each node. 
Find the subset of nodes in the tree whose sum give you the maximum weight such that if a node is in the subset, 
none of its children can be in that subset.

*/

class BTNode
{
public:
    BTNode(int w):weight(w), loss(0), left(null), right(null) {}

    void MarkLoss()
    {
        int lloss=0, rloss=0;
        if(left)
        {
            left->MarkLoss();
            lloss = left->GetLoss();
        }
        if(right)
        {
            right->MarkLoss();
            rloss = right->GetLoss();
        }
        if(rloss+lloss < weight)
        {
            loss = weight - (rloss+lloss);
            if(left) left->ForceLoss(0); //check for null
            if(right) right->ForceLoss(0);//check for null
        }

    }

    int GetLoss() { return loss; }

    void ForceLoss(int l) { loss = l; }

    void GetChildrenCausingLoss(vector<BTNode*>& list)
    {
        if(left)
            left->GetChildrenCausingLoss(list);
        if(right)
            right->GetChildrenCausingLoss(list);
        if(loss > 0)
            list.push_back(this);
    }

private:
    int weight;
    int loss;
    BST* left;
    BST* right;
};

int main()
{
    BTNode* root = CreateBT();

    root->MarkLoss();
    vector<BTNode*> max_weigh_list;
    root->GetChildrenCausingLoss(max_weigh_list);

    return 0;
}

- Raghav September 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Actually ForceLoss should be like below:

void ForceLoss(int l) 
    { 
        loss = l; 
        if(l == 0)
        {
            //reassign the loss to children
            if(left) left->MarkLoss(); //may be loss of node can be cached and reuse to optimize using DP
            if(right) right->MarkLoss();//may be loss of node can be cached and reuse to optimize using DP
        }
    }

- Raghav September 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is the solution of this problem :

private static int getMaxWeight(Node root) {

        int a = 0;
        int b = 0;

        if (root == null) {
            return 0;
        }

        if (root.left != null) {
            a = getMaxWeight(root.left.left) + getMaxWeight(root.left.right);
        }

        if (root.right != null) {
            b = getMaxWeight(root.right.left) + getMaxWeight(root.right.right);
        }


        int x = root.val + a + b;
        int y = getMaxWeight(root.left) + getMaxWeight(root.right);

        if (x >= y) {
            return x;
        } else {
            return y;
        }

    }

- mail.kshitij.jain March 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Solve(root) = MAX { (1 + sum of Solve for all grandchildren of root),
(sum of Solve for all children of root) }

- manaskirti June 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Solve the problem recursively..

- Anonymous January 14, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

This is a typical maximum subset sum problem. We can start as follows first we find all the sums of a specific level and put it in an array..
so for example A[]={2,7,6,9....}..where A[0]->sum of nodes at level 0 A[1]->sum of nodes at level1.
After this we will use DP to find the max subsetsum of A[] with a constraint that no to consecutive element can be in that subset.

- Anonymous September 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

it is not maximum subset sum problem.

- pankaj March 11, 2011 | 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