Google Interview Question for Senior Software Development Engineers


Team: TEZ
Country: India
Interview Type: Phone Interview




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

public class TreeNode 
{
	private int data;
	private TreeNode leftChild;
	private TreeNode rightChild;
	
	public TreeNode(int data)
	{
		this.data = data;
		this.leftChild = null;
		this.rightChild = null;
	}
	
	public TreeNode(int data, TreeNode left, TreeNode right)
	{
		this.data = data;
		this.leftChild = left;
		this.rightChild = right;
	}
	
	public int getData()
	{
		return this.data;
	}
	
	public void setLeftChild(TreeNode node)
	{
		this.leftChild = node;
	}
	
	public TreeNode getLeftChild()
	{
		return this.leftChild;
	}
	
	public void setRightChild(TreeNode node)
	{
		this.rightChild = node;
	}
	
	public TreeNode getRightChild()
	{
		return this.rightChild;
	}
}



public class TreeNodeSum 
{
	public long recursiveSum(TreeNode root)
	{
		if(root == null)
			return 0;
		else
			return root.getData() + recursiveSum(root.getLeftChild()) + recursiveSum(root.getRightChild());
	}
	
	public long sum(TreeNode root)
	{
		if(root == null)
			return 0;
		else
		{
			long sum = root.getData();
			Queue<TreeNode> bfsQueue = new LinkedList<TreeNode>();
			TreeNode currentNode;
			
			if(root.getLeftChild() != null)
			{
				bfsQueue.add(root.getLeftChild());
			}
			
			if(root.getRightChild() != null)
			{
				bfsQueue.add(root.getRightChild());
			}
			
			while(!bfsQueue.isEmpty())
			{
				currentNode = bfsQueue.remove();
				sum += currentNode.getData();
				
				if(currentNode.getLeftChild() != null)
				{
					bfsQueue.add(currentNode.getLeftChild());
				}
				
				if(currentNode.getRightChild() != null)
				{
					bfsQueue.add(currentNode.getRightChild());
				}
			}
			
			return sum;
		}
	}
}

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

Any tree-walk algorithm can find the solution and it depends on the application. The run time is O(n) and the memory is O(n).

- nooreddin February 14, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
C++ solution: {{{ Struct Node { Int data = 0; Node *l = nullptr; Node *r = nullptr; }; Int SumAllNodes(Node *root) { If (root == nullptr) { Return 0; } Auto sumL = SumAllNodes(root->l); Auto sumR = SumAllNodes(root->r); Return (sumL + sumR + root->data); } }} - the_lobster February 19, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static class TreeNode {
        int data;
        ArrayList<TreeNode> children;
        TreeNode(int d) {
            data = d;
            children = new ArrayList<>();
        }
}
public static int getSum(TreeNode root) {
        if (root == null) return 0;
        Queue<TreeNode> q = new LinkedList<>();
        q.add(root);
        int sum = 0;
        while (q.size() != 0) {
            TreeNode n = q.remove();
            ArrayList<TreeNode> array = n.children;
            sum += n.data;
            for (TreeNode t : array) {
                q.add(t);
            }            
        }
        return sum;
}

- Aim_Google April 02, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Tree:

    class Node:

        def __init__(self, val):
            self.val = val
            self.lft, self.rgt = None, None

        def __iter__(self):
            yield self
            if self.lft: yield from self.lft.__iter__()
            if self.rgt: yield from self.rgt.__iter__()

    def __init__(self, vals = []):
        self._root = None
        for val in vals:
            self.add(val)

    def __iter__(self):
        return self._root.__iter__() if self._root else iter(())

    def add(self, val):
        self._add(Tree.Node(val), self._root)

    def _add(self, node, parent):
        if parent:
            if node.val <= parent.val:
                if parent.lft:
                    self._add(node, parent.lft)
                else:
                    parent.lft = node
            else:
                if parent.rgt:
                    self._add(node, parent.rgt)
                else:
                    parent.rgt = node
        else:
            self._root = node
        return node

def sum_nodes(tree) -> int:
    t = 0
    for n in tree:
        t += n.val
    return t


if __name__ == '__main__':
    assert sum_nodes(Tree(range(10))) == sum(range(10))
    assert sum_nodes(Tree()) == 0

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

int sum(node *node, int *totalSum)
{
if(node == NULL){
return 0;
}
*totalSum += node->data;

sum(node->left, totalSum);
sum(node->right, totalSum);

return *totalSum;
}

- SAMMYDEK April 13, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int sum(node *node, int *totalSum)
{
        if(node == NULL){
                return 0;
        }
        *totalSum += node->data;

        sum(node->left, totalSum);
        sum(node->right, totalSum);

        return *totalSum;
}

- SAMMYDEK April 13, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int sum(node *node, int *totalSum)
{
        if(node == NULL){
                return 0;
        }
        *totalSum += node->data;

        sum(node->left, totalSum);
        sum(node->right, totalSum);

        return *totalSum;
}

- SAMMYDEK April 13, 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