Google Interview Question for Front-end Software Engineers






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

More compact version without recursion. Bottom up strategy:

public class TriangleMaxSum {
	
	private Integer[][] maxSum;
	
	public int maxSum(int[][] triangle) {
		maxSum = new Integer[triangle.length][];
		for (int r=triangle.length-1;r>=0;--r) {
			maxSum[r] = new Integer[triangle[r].length];
			for (int c=0; (c < triangle[r].length); ++c) {
				maxSum[r][c] = triangle[r][c];
				if (r != (triangle.length-1)) {
					maxSum[r][c] += Math.max(maxSum[r+1][c], maxSum[r+1][c+1]);
				}
			}
		}
		return maxSum[0][0];
	}

}

- lucas.eustaquio August 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

1) use the standard A.P. formula- n(n+1)/2
2) This is the standard Google tree vertical sum problem.Logic is root->data + root->(left->right->data) until left->right is null. This sum would be the first level sum for the root node.

- rahul July 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Solving using DP (Same solution as coderanjiy666, but with caching):
Space and Time will be O(n), since each element path is calculated just once.

public class TriangleMaxSum {
	
	private int[][] triangle;
	private Integer[][] maxSum;
	
	public TriangleMaxSum(int[][] triangle) {
		this.triangle = triangle;
		maxSum = new Integer[triangle.length][];
		for (int i=0; i<maxSum.length; ++i) {
			maxSum[i] = new Integer[triangle[i].length];
		}
	}
	
	public int maxSum() {
		return maxSum(0,0);
	}
	
	private int maxSum(int r, int c) {
		if (r >= maxSum.length)
			return 0;
		
		if (maxSum[r][c] != null)
			return maxSum[r][c];
		
		maxSum[r][c] = triangle[r][c] + Math.max(maxSum(r+1, c), maxSum(r+1, c+1));
		return maxSum[r][c];
	}

}

public class TriangleMaxSumTest {

	private int[][] createRandomTriangle(int rows) {
		Random rand = new Random();
		int[][] triangle = new int[rows][];
		for (int cols = 1, r=0; r<rows; ++r, ++cols) {
			triangle[r] = new int[cols];
			for (int c = 0; c<cols; ++c) {
				triangle[r][c] = rand.nextInt(100);
			}
		}
		return triangle;
	}
	
	@Test
	public void testMaxSum() {
		int[][] triangle1 = createRandomTriangle(1000);
		TriangleMaxSum maxSum = new TriangleMaxSum(triangle1);
		System.out.println("Max: " + maxSum.maxSum());
	}

}

- lucas.eustaquio August 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this is the right answer (with necessary caching).

- yemrus_ank October 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

function int maxsum(node root)
{
if(null == root)
{
return 0;
}
maxleft=maxsum(root->left);
maxrt=maxsum(root->right);

if(maxrt>maxleft)
{
return maxrt+root.value;
}
else
{
return maxleft+root.value;
}
}

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

function int maxsum(node root)
{
if(null == root)
{
return 0;
}
maxleft=maxsum(root->left);
maxrt=maxsum(root->right);

if(maxrt>maxleft)
{
return maxrt+root.value;
}
else
{
return maxleft+root.value;
}
}

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

its involves maths stuff rather then considering tree (it wont work check below link for detail ) or applying computer science & problem is already famous (Euler problem ) as we have to find the maximum sum in triangle we have given a big triangle which has many small triangle only thing u need to know a triangle has 3 vertexes so that you can approach :)

and a detailed description,explaination & solution you can find here

shashank7s dot blogspot dot com/2011/04/project-euler-problem-67-finding dot html

p.s. remove dot & place . above then concate to remove spaces

let me know if i missed something or any other approach to solve it

Shashank

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

seems like a DP problem.

- Iqbal Singh July 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Js solution

var arr = [[1], [2, 3] , [5, 6, 17], [20, 30, 400, 50]];
function Tree(depth, value, position){
	this.value = value;	
	
	depth++;
	if(depth >= arr.length){
	    this.max_value = this.value;
		return;
	}
	this.left = new Tree(depth, arr[depth][position], position);
	this.right = new Tree(depth, arr[depth][position + 1], position + 1);	
	this.max_value = this.value + Math.max(this.right.max_value, this.left.max_value);
}
var depth = 0;
tree = new Tree(depth, arr[0][0], 0);
console.log(tree.max_value);

- Dmytro February 08, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Formula is: n(n+1) / 2 or n(n-1) / 2 + n
Javascript solution:

const maxPathSum = (triangle, depth=0, position=0) => {
  const level = triangle[depth];
  if (level == null) { return 0; }
  
  const leftSum = maxPathSum( triangle, depth+1, position );
  const rightSum = maxPathSum( triangle, depth+1, position+1 );
  
  return level[position] + Math.max( leftSum, rightSum );
}

const triangle = [ [0], [1, 2], [5, 4, 8] ];
console.log( maxPathSum( triangle ) );

- Oleg Solomka October 07, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var maxPathSum = function(root) {
    
    let maxVal = -Infinity;
    findSum(root);
    return maxVal;
    
    function findSum(root) {
        if(root === null) return 0;
        let l = Math.max(findSum(root.left), 0);
        let r = Math.max(findSum(root.right), 0);
        let maxlr = Math.max(l, r);
        let maxSingle = Math.max(maxlr + root.val, root.val );
        let maxAll = Math.max(maxSingle,l + r + root.val);
        maxVal = Math.max.call(null, maxVal, maxSingle, maxAll);
        return maxSingle;
    } 
};

- dtallinterviewee December 07, 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