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


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