Yahoo Interview Question for Software Engineer / Developers






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

The depth of a tree is the length of the longest path from the root to any of the leaves.

So, the following recursive function should do when applied on root:
depth(inner_node) = max(
depth(inner_node->left_child),
depth(inner_node->right_child)
)
+ 1
and
depth(leaf) = 0

- kay June 08, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int Treedepth(tree* T)
{
if(T==NULL)
{
return -1;
}
return (1+max(Treedepth(T->left),Treedepth(T->right));
}

- newbie June 09, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int maxdepth(tree_node * root)
{
	int d1=0, d2=0;
	if (root == NULL)
		return 0; 
	d1 = maxdepth(root->left);
	d2 = maxdepth(root->right);
	if(d1>d2)
		return d1+1;
	else 
		return d2+1;
}

- gauravk.18 February 29, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can anyone convert this algorithms to a no recursive one ?

- Algo_maniac September 11, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

no recursion..probably..use graph traversal to count the number of nodes(=x) and take log()x or log(x)+1.

- genehacker September 11, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this does not work. The tree may not be balanced.

- redroof November 17, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

BFS and keep track of levels it traveled

- Anonymous December 29, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Do a level order traversal or BFS, and count the levels,

typedef struct node{
int data;
struct node *right;
struct node *left;
}Node;

typedef struct {
Node *p;
int level;
}queue;

void levelOrder(Node *ptr) {
if (ptr == NULL) return;
while(!isEmpty() ) {
queue *q = dequeue();
q->level++;
if (q->p->left != NULL) enqueue (q->p->left,q->level);
if (q->p->right != NULL) enqueue (q->p->right,q->level);
}
return ;

}

- Anonymous March 03, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

enqueue(ptr,1); // before while in the above code

- Anonymous March 03, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int findDepth(struct node* T)
{
if(T==NULL)return 0;

else
return(1+max( findDepth(T->left), findDepth(T->right) );

}

- Anonymous August 22, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

can this be done in O(h) h is the height of the tree.??

- Mridul Malpani October 23, 2010 | 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