Facebook Interview Question for Android Engineers


Country: United States
Interview Type: Phone Interview




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

public static int getMaxPathSum(Node root) {
		int suml = 0;
		int sumr =0;
		if (root.l == null && root.r == null) return root.val;
		if (root.l != null ) {
			suml = suml + root.val +  getMaxPathSum(root.l);
		}
		if (root.r != null ) {
			sumr = sumr + root.val + getMaxPathSum(root.r);
		}
		
		return Math.max(suml, sumr);

}

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

public static int getMaxPathSum(Node root) {
int suml = 0;
int sumr =0;
if (root.l == null && root.r == null) return root.val;
if (root.l != null ) {
suml = suml + root.val + getMaxPathSum(root.l);
}
if (root.r != null ) {
sumr = sumr + root.val + getMaxPathSum(root.r);
}

return Math.max(suml, sumr);
}

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

public static int getMaxPathSum(Node root) {
		int suml = 0;
		int sumr =0;
		if (root.l == null && root.r == null) return root.val;
		if (root.l != null ) {
			suml = suml + root.val +  getMaxPathSum(root.l);
		}
		if (root.r != null ) {
			sumr = sumr + root.val + getMaxPathSum(root.r);
		}
		
		return Math.max(suml, sumr);

}

- Imad May 07, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

function getMaxPathSum(Tree) {
   const leftNode = Tree.left;
   const rightNode = Tree.right;
   if (leftNode === null && rightNode === null) {
      return Tree.value;
   }
   const sumLeft = Tree.value + getMaxPathSum(leftNode);
   const sumRight = Tree.value + getMaxPathSum(rightNode);
   if (sumLeft >= sumRight) {
      return sumLeft;
   }
   return sumRight;
}

- mgonyan May 30, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Well, I think that you can have a path which doesn't necessarily go through the root...That means that we have to find the path between any two nodes which constitutes the maximum sum...The below code achieves that

def find_height_by_value(node):
    if node is None:
        return 0
    return node.data + max(find_height_by_value(node.left), find_height_by_value(node.right))

def find_max_sum(node):
    if node is None:
        return 0
    lheight = find_height_by_value(node.left)
    rheight = find_height_by_value(node.right)

    lmax = find_max_sum(node.left)
    rmax = find_max_sum(node.right)

    return max(max(lmax, rmax), node.data + lheight + rheight)

root = Node(4)
root.left = Node(3)
root.right = Node(5)
root.left.left = Node(2)
root.right.right = Node(6)
print("MAXIUMUM SUM = ",find_max_sum(root))

The output for the above test case is 20...

- Devansh Singh July 24, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def find_height_by_value(node):
    if node is None:
        return 0
    return node.data + max(find_height_by_value(node.left), find_height_by_value(node.right))

def find_max_sum(node):
    if node is None:
        return 0
    lheight = find_height_by_value(node.left)
    rheight = find_height_by_value(node.right)

    lmax = find_max_sum(node.left)
    rmax = find_max_sum(node.right)

    return max(max(lmax, rmax), node.data + lheight + rheight)


root = Node(4)
root.left = Node(3)
root.right = Node(5)
root.left.left = Node(2)
root.right.right = Node(6)
print("MAXIUMUM SUM = ",find_max_sum(root))

The path can be between any two nodes constituting the maximum sum...It doesn't necessarily has to pass through the root node.

- Devansh Singh July 24, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Solution(object):
    def maxPathSum(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        self.res = float('-inf')
        self.dfs(root)
        return self.res

    def dfs(self, node):
        if not node: return 0
        l = self.dfs(node.left)
        if l < 0: l = 0
        r = self.dfs(node.right)
        if r < 0: r = 0
        if l+r+node.val > self.res:
            self.res = l+r+node.val
        return node.val + max(l,r)

- aasimon@uc.cl August 11, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*
Given a binary tree, where each node represents an integer, 
find the max value of path sum.
*/

/*
Clarification:

Can I have negative values as nodes values? YES
Can I assume that all the numbers are integer? YES
Should the root be on the path? NO

Brute force: For every every node, I'll consider this nodes as the start node
and I'll try with the rest of the nodes as the finish node.

Time complexity is O(N*N)

Better solution is: For every node, has a value that is the max path that start
in that node and goes to the subtree.

Then, we can compare and merge this values to get the final answer.

*/

class BTree{
 public:
	long long value,sum;
	BTree * left, * right;
	BTree(long long _value, BTree * _left, BTree * _right){
		value=_value; 
		left=_left;
		right=_right;
		sum=max({0LL,value,getSum(left)+value,getSum(right)+value});
	}
	static long long maxPath(BTree * tree){
		if(!tree)return 0;
		long long ans=max({tree->sum,maxPath(tree->left),maxPath(tree->right)});
		ans=max(ans,tree->value+getSum(tree->left)+getSum(tree->right));
		return ans;
	}
	static long long getSum(BTree * tree){
		if(!tree)return 0;
		return tree->sum;
	}
};

int main() {FIN;
	BTree f(2,NULL,NULL);
	BTree g(1,NULL,NULL);
	BTree c(3,&f,&g);
	BTree d(4,NULL,NULL);
	BTree e(5,NULL,NULL);
	BTree b(3,&d,&e);
	BTree a(2,&b,&c);
	cout<<BTree::maxPath(&a)<<"\n";
}

- mrz August 29, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*
Given a binary tree, where each node represents an integer, 
find the max value of path sum.
*/

/*
Clarification:

Can I have negative values as nodes values? YES
Can I assume that all the numbers are integer? YES
Should the root be on the path? NO

Brute force: For every every node, I'll consider this nodes as the start node
and I'll try with the rest of the nodes as the finish node.

Time complexity is O(N*N)

Better solution is: For every node, has a value that is the max path that start
in that node and goes to the subtree.

Then, we can compare and merge this values to get the final answer.

*/

class BTree{
 public:
	long long value,sum;
	BTree * left, * right;
	BTree(long long _value, BTree * _left, BTree * _right){
		value=_value; 
		left=_left;
		right=_right;
		sum=max({0LL,value,getSum(left)+value,getSum(right)+value});
	}
	static long long maxPath(BTree * tree){
		if(!tree)return 0;
		long long ans=max({tree->sum,maxPath(tree->left),maxPath(tree->right)});
		ans=max(ans,tree->value+getSum(tree->left)+getSum(tree->right));
		return ans;
	}
	static long long getSum(BTree * tree){
		if(!tree)return 0;
		return tree->sum;
	}
};

int main() {FIN;
	BTree f(2,NULL,NULL);
	BTree g(1,NULL,NULL);
	BTree c(3,&f,&g);
	BTree d(4,NULL,NULL);
	BTree e(5,NULL,NULL);
	BTree b(3,&d,&e);
	BTree a(2,&b,&c);
	cout<<BTree::maxPath(&a)<<"\n";
}

- mrz August 29, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*
Given a binary tree, where each node represents an integer, 
find the max value of path sum.
*/

/*
Clarification:

Can I have negative values as nodes values? YES
Can I assume that all the numbers are integer? YES
Should the root be on the path? NO

Brute force: For every every node, I'll consider this nodes as the start node
and I'll try with the rest of the nodes as the finish node.

Time complexity is O(N*N)

Better solution is: For every node, has a value that is the max path that start
in that node and goes to the subtree.

Then, we can compare and merge this values to get the final answer.

*/

class BTree{
 public:
	long long value,sum;
	BTree * left, * right;
	BTree(long long _value, BTree * _left, BTree * _right){
		value=_value; 
		left=_left;
		right=_right;
		sum=max({0LL,value,getSum(left)+value,getSum(right)+value});
	}
	static long long maxPath(BTree * tree){
		if(!tree)return 0;
		long long ans=max({tree->sum,maxPath(tree->left),maxPath(tree->right)});
		ans=max(ans,tree->value+getSum(tree->left)+getSum(tree->right));
		return ans;
	}
	static long long getSum(BTree * tree){
		if(!tree)return 0;
		return tree->sum;
	}
};

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

/*
Given a binary tree, where each node represents an integer, 
find the max value of path sum.
*/

/*
Clarification:

Can I have negative values as nodes values? YES
Can I assume that all the numbers are integer? YES
Should the root be on the path? NO

Brute force: For every every node, I'll consider this nodes as the start node
and I'll try with the rest of the nodes as the finish node.

Time complexity is O(N*N)

Better solution is: For every node, has a value that is the max path that start
in that node and goes to the subtree.

Then, we can compare and merge this values to get the final answer.

*/

class BTree{
 public:
	long long value,sum;
	BTree * left, * right;
	BTree(long long _value, BTree * _left, BTree * _right){
		value=_value; 
		left=_left;
		right=_right;
		sum=max({0LL,value,getSum(left)+value,getSum(right)+value});
	}
	static long long maxPath(BTree * tree){
		if(!tree)return 0;
		long long ans=max({tree->sum,maxPath(tree->left),maxPath(tree->right)});
		ans=max(ans,tree->value+getSum(tree->left)+getSum(tree->right));
		return ans;
	}
	static long long getSum(BTree * tree){
		if(!tree)return 0;
		return tree->sum;
	}
};

- mrz August 29, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int maxPath(Node node) {
if(node==null){
return 0;
}
return Math.max(node.data+maxPath(node.left),node.data+maxPath(node.right));
}

- simplest October 03, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int maxPath(Node node) {
        if(node==null){
            return 0;
        }
        return Math.max(node.data+maxPath(node.left),node.data+maxPath(node.right));
    }

- simplest October 03, 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