Yahoo Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

private static BinaryTreeNode deepestNode(BinaryTreeNode root) {
		BinaryTreeNode temp = null;
		Queue<BinaryTreeNode> q = new LinkedBlockingDeque<BinaryTreeNode>();
		q.add(root);
		while (!q.isEmpty()) {
			temp = q.poll();
			if (temp.getLeft() != null)
				q.add(temp.getLeft());
			if (temp.getRight() != null)
				q.add(temp.getRight());
		}

		return temp;
	}

- Vir Pratap Uttam May 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
3
of 3 vote

Item* get_deepest_node(Item* root) {
  int max_level = -1;
  Item* node = NULL;
  get_deepest_node(root, 0, &max_level, &node);
  return node;
}

void get_deepest_node(Item* root, int curr_level, int* max_level, Item** node) {
  if (!root)
    return;
  if (curr_level > *max_level) {
    *max_level = curr_level;
    *node = root;
  }
  get_deepest_node(root->left, curr_level+1, max_level, node);
  get_deepest_node(root->right, curr_level+1, max_level, node);
}

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

Maybe in these sort of questions the interviewer expects you to ask. What if there are multiple nodes at the same depth which zporgd's solution totally ignores

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

#include<iostream>
using namespace std;

struct node
{
int val;
node *left;
node *right;
}*root, *dNode;
int dlevel = 0;

node *creategraph(int k)
{
node *temp= new node;
temp->val=k;
temp->left=NULL;
temp->right=NULL;
return temp;
}

void deepestNode(node *r, int level)
{
if(r==NULL)
return;
if(dlevel < level){
dNode = r;
dlevel = level;
}
deepestNode(r->left, level+1);
deepestNode(r->right, level+1);
}

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

Why not using level-order Traversal. the last level is the deepest nodes

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

You can do that, but space usage will be high.

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

No, the space won't be high. I think BFS or a level order is the best solution.
You have to use O(n) space anyway. Recursion is worse (stack space takes up O(n)) and if your tree is deep, you will get stack overflow error.

- im.code.warrior July 19, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is my solution.

public  BNode DeepestNode(BST bst)
        {
            Queue<BNode> q = new Queue<BNode>();
            BNode deepest = null;
            q.Enqueue(bst.Root);

            while(q.Count() > 0)
            {
                deepest = q.Dequeue();
                if(deepest.LeftChild != null)
                {
                    q.Enqueue(deepest.LeftChild);
                }
                if(deepest.RightChild != null)
                {
                    q.Enqueue(deepest.RightChild);
                }
            }

            return deepest;
        }

- Anonymous July 16, 2014 | 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