Google Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

doing bfs the last node which will be traversed is the rightmost node at maximum depth

- ashu26cs October 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

you are right

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

Doing BFS takes linear time. Instead, if we augment each of our tree nodes to hold the number of nodes in the tree rooted at that node (by doing little extra work during insertion), we could just check if case 1: more nodes are reachable via the left subtree of a root. case 2: more nodes are reachable via the right subtree of a root. In case one, we find the predecessor of the root, in case two we find the node with the max key value in the tree. This solution takes logarithmic time. Apologies if my solution is incorrect.

- Anonymous November 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No, I take that back. There could be scenarios where, with fewer nodes, right subtree of the root could grow to be deeper that the left subtree. So the solution I proposed doesnt categorically work for all trees.

- Anonymous November 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Can you define with an example what right most node at maxDepth with a simple example?

- bleep October 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

1
                         2        3
                       4  5   6   7

Max Depth in this case is 3 ( root node inclusive ) . Right most node at Depth 3 is 7 .

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

test

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

here is the solution

Node* getRightmostAtDepth(Node* n, int depth, bool youAreLeft)
{
	static int currDepth = 0;

	if(depth == currDepth)
	{ 
		if( youAreLeft == false)
			return n;
		else return null;
	}

	if(n == null)
		return null;

	currDepth++;

	Node* R =  getRightmostAtDepth(n->right, depth, false);

	Node* L = getRightmostAtDepth(n->left, depth, true);

	if(R != null)
	{
		return R;
	}
	else if (L ! = null)
	{
		return L;
	}
	else
	{
		return null;
	}
}

main()
{
	...
	getRightmostAtDepth(head, 10, false);
}

- coldskull October 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

O(n) time recursive

private Pair<Tree.Node, Integer> getDeepestAndRightMostNode(
    Tree.Node node, int ht) {
    if (node == null) {
        return null;
    }
    if (node.left == null && node.right == null) {
        return new Pair<Tree.Node, Integer>(node, ht);
    }
    Pair<Tree.Node, Integer> lResult =
        getDeepestAndRightMostNode(node.left, ht + 1);
    Pair<Tree.Node, Integer> rResult =
        getDeepestAndRightMostNode(node.left, ht + 1);
    if (lResult != null && rResult != null) {
        if (lResult.second > rResult.second) {
            return lResult;
        } else {
            return rResult;
        }
    }
    return (lResult == null) ? rResult : lResult;
}

// Assuming tree is not empty
public Tree.Node getDeepestAndRightMostNode(Tree tree) {
    return getDeepestAndRightMostNode(tree.root, 1).first;
}

- rix October 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Node{
 Node left;
 Node right;
 int data;
};
class BarNode(
 Node max;
 int depth;
};
Node foo(Node root){
 if(root==null)
  return(null);
 BarNode maxNode = bar(root, 1);
 if(maxNode==null)
  return(null);
 else
  return(maxNode.max);
}
BarNode bar(Node root, int depth){
 if(root==null)
  return(null);
 if(root.left==null && root.right==null){
  BarNode bNode = new BarNode();
  bNode.depth = depth;
  bNode.max = root;
  return(bNode);
 }
 lBNode = bar(root.left, depth+1);
 rBNode = bar(root.right, depth+1);
 if(lBNode==null)
  return(rBNode);
 if(rBNode==null)
  return(rBNode);
 if(rBNode.depth>=lBNode.depth) // Prefers right node to left if they are at the same depth
  return(rBNode);
 return(lBNode); 
}

- CodeSpace October 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

How about this tree. BFS will give 5 but we want 3 as the answer

0
        1                            2
              3                  4
                             5

- Intern October 31, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

From the question "( ties to the right means , right most node at MaxDepth D )". If I understand this statement correctly then, 5 will be the right most node at maximum depth.

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

Someone has already suggested this, I am just clarifying:
Do a BFS and at each step insert the left node before right node in the queue. The last node in the queue would be the answer.
How to check if an element is last element in the queue? When you pop element from the queue, if the popped element has no children and queue is empty after popping it, you have found the last element.

- Epic_coder October 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

BFS.. last node is the right most node with mxdepth

- coderGirl October 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How's this one


class Tree{
Node root;
int rightMost=0;
int maxHeigth=0;
Node rightMostDeep;
public void findRightMostDepth(){
findRightMostDepthRecursive(root,0,0);
}

private void findRightMostDepthRecursive(Node temp,int x,int y){
if(temp!=null){
if(x>rightMost){
maxDepth = y;
rightMost = x;
rightMostdeep = temp;
}
findRightMostDepthRecursive(temp.left,x-1,y+1);
findRightMostDepthRecursive(temp.right,x+1,y+1);
}
}
}

- Anonymous October 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What i am trying here is to do a preorder traversal and mark each left as -1 and each right as +1 and noting the position of RightMost and MAxDepth

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

void findRightMostHelper (Node* p, int& maxDepth, Node*& rNode, int depth){
  if (p == NULL) return;
  if (depth >= maxDepth){
    maxDepth = depth;
    rNode = p;
  }
  findRightMostHelper(p->left, maxDepth, rNode, depth+1);
  findRightMostHelper(p->right, maxDepth, rNode, depth+1);
}

int findRightMost (Node* p){
  int maxDepth = 0;
  Node* rNode = NULL;
  findRightMostHelper(p, maxDepth, rVal, 0);
}

- Johny October 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

call to findRightMostHelper(p->right, ....) should be coming first before p->left as we need to find the rightmost node.

- DashDash March 05, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I do not think that the problem is to find right most node.

If it is wrong then I guess the question would be
'Find a node right most node at depth D'
0
0 0
2 1 3 4
5 6
if the input is 3 then the answer would be 4 because the right most node at level 3 is 4.

now it is close to google interview question.

- HS November 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think the problem is to find the rightmost node at given depth, as suggested there can be multiple solutions to this problem:
1. BFS O(n), average, worst case.
2. Traverse the tree check at any given level if node->right exists (offcourse when node.depth < D) , if yes recursively call routine to find if there exist a node at depth D. If not repeat the same for node.left.

Node *findNode(Node *root, int depth, const int D, Node **ret)
{
     if (*ret) {
          return; // terminate recursion
     }
     if (depth < 0) {
         return NULL; // invalid arguments.
     }
     if (depth == D) {
          *ret = root;
           return ;
     } else if (*depth > D) {
        assert (0, "we should never get here");
     }
     findNode(root->right, depth+1, D, ret);
     findNode(root->left, depth+1, D, ret);
 }

- Feedback December 02, 2012 | 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