Yahoo Interview Question for Software Engineer / Developers


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

- Anonymous July 11, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- avengers July 13, 2014 | Flag Reply
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;
}

- Anonymous October 14, 2014 | Flag Reply
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)));

}

- Java solution March 07, 2016 | Flag Reply
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)));
	}

- Java solution March 07, 2016 | Flag Reply
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)));
	}

- bhavi March 07, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

return treeNode.value;

}

- Pras August 29, 2016 | Flag Reply
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);
}

- dr.house July 11, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

incorrect arguments in _traverse recursive call

- maverick July 11, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thanks. Corrected

- dr.house July 13, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Where does 'maxDepth' get updated?

- Hari Prasad Perabattula July 15, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

updated. should be clearer now.

- dr.house July 15, 2014 | Flag


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