## Yahoo Interview Question for Software Engineer / Developers

• 1
of 1 vote

Country: United States
Interview Type: Phone Interview

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

PYTHON.

``````def max_depth(node) {
if node == None:
return (None, 0);
left, left_depth = max_depth(node.left())
right, right_depth = max_depth(node.right())
if left_depth == 0 and right_depth == 0:
return node, 1
if left_depth >= right_depth:
return left, left_depth+1
return right, right_depth+1``````

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

use queue for level order traversal and print the last element .

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

``````int deepestNode(tnode *tree){
int front, rear;
struct tnode** queue = createQueue(&front,&rear);
struct tnode *temp = tree;
struct tnode *prev = tree;
while(temp!=NULL){
prev = temp;
if(temp->left!=NULL){
enQueue(queue,&rear,temp->left);
}
if(temp->right!=NULL){
enQueue(queue,&rear,temp->right);
}
temp = deQueue(queue, &front);
}
return prev->value;
}``````

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

``````public void deepest() {
int h = findHeightofTree(root);
findDeepestNode(root,h, 0);

}

private void findDeepestNode(Node root, int h, int depth) {
if(root==null)
return ;
if(root.left==null && root.right==null )
{
if(depth+1 == h)
System.out.println(root.data);
}
findDeepestNode(root.left, h, depth+1);
findDeepestNode(root.right, h, depth+1);
}

private int findHeightofTree(Node root) {
if(root == null)
return 0;

return (1+Math.max(findHeightofTree(root.left), findHeightofTree(root.right)));``````

}

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

``````public void deepest() {
int h = findHeightofTree(root);
findDeepestNode(root,h, 0);

}

private void findDeepestNode(Node root, int h, int depth) {
if(root==null)
return ;
if(root.left==null && root.right==null )
{
if(depth+1 == h)
System.out.println(root.data);
}
findDeepestNode(root.left, h, depth+1);
findDeepestNode(root.right, h, depth+1);
}

private int findHeightofTree(Node root) {
if(root == null)
return 0;

return (1+Math.max(findHeightofTree(root.left), findHeightofTree(root.right)));
}``````

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

``````public void deepest() {
int h = findHeightofTree(root);
findDeepestNode(root,h, 0);

}

private void findDeepestNode(Node root, int h, int depth) {
if(root==null)
return ;
if(root.left==null && root.right==null )
{
if(depth+1 == h)
System.out.println(root.data);
}
findDeepestNode(root.left, h, depth+1);
findDeepestNode(root.right, h, depth+1);
}

private int findHeightofTree(Node root) {
if(root == null)
return 0;

return (1+Math.max(findHeightofTree(root.left), findHeightofTree(root.right)));
}``````

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

public static int deepestNode(TreeNode node) {
// int depth = 0;
TreeNode treeNode = null;
while (q.isEmpty() != true) {
// ++depth;
treeNode = q.remove();
if (treeNode.left != null)
if (treeNode.right != null)
}
System.out.println(treeNode.value);

return treeNode.value;

}

Comment hidden because of low score. Click to expand.
-1
of 1 vote

Another attempt in Java. Note that only a leaf node can ever be the deepest node.

``````class NodeHolder {
Node node;
int maxDepth;
}

void traverse(Node root) {
NodeHolder holder = new NodeHolder();
_traverse(root, 0, holder);
return holder.node;
}

void _traverse(Node root, int depth, NodeHolder deepest) {
if (root == null) return;
if (root->left == null && root->right == null) {
if (depth > deepest.maxDepth) {
deepest.node = root;
deepest.maxDepth = depth;
}
}
_traverse(root->left, depth + 1, deepest);
_traverse(root->right, depth + 1, deepest);
}``````

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

incorrect arguments in _traverse recursive call

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

Thanks. Corrected

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

Where does 'maxDepth' get updated?

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

updated. should be clearer now.

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.

### 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.