Facebook Interview Question for Software Engineer / Developers






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

Asymptotically they will be the same though BFS can be written to stop as soon as a leaf node is found in which case it will not explore any nodes that are at a level below the leaf node with min depth.
which even in the worst case will explore half the nodes less... (since a tree with n nodes has atleast n/2 nodes as leaves and BFS can stop at the first leaf itself)
whereas you cannot say anything about the performance of a DFS since the leaf with min depth may be explored at the very end.

- Bhavik October 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

I think I am wrong regarding the n/2 nodes since that is only the case for a complete binary tree.
However I am atleast sure about this, the BFS approach will be better than the DFS approach, since BFS need not explore the entire tree in most cases whereas DFS will have to explore the entire tree in any case...

- Bhavik October 30, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

I would support BFS. Thats because BFS will stop as soon as it sees a node at a level with no children. But DFS would anyway have to traverse until every leaf node and then find which is the smallest path to a leaf node.
Comparing the complexities depends on the way the tree is constructed. So either might win. But on an average, BFS would win.

- Anonymous November 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is the recursive implementation.

int min=Integer.MAX_VALUE;
void findMinDeapth(node root,int depth)
{
	if(root==null)
	{
		if(depth<min)
			min=depth;
		return;
	}
	findMinDeapth(root.left,depth+1);
	findMinDeapth(root.right,depth+1);
}

But i think BFS will be more efficient .

- gulusworld1989 October 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I tested, this code doesn't work.

- Anonymous October 24, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

a sligth change could fix it....

int min=Integer.MAX_VALUE;
void findMinDeapth(node root,int depth)
{
if(root==null)
return;

if (root.left == NULL && root.right == NULL )
{
if(depth<min)
min=depth;
return;
}
findMinDeapth(root.left,depth+1);
findMinDeapth(root.right,depth+1);
}

- iatbst February 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is the BFS approach

int findMinDeapth(node root)
{
	Queue<root> q1=new LinkedList<node>();
	Queue<root> q2=new LinkedList<node>();
	q1.add(root);
	int depth=0;
	while(!q1.isEmpty() || !q2.isEmpty())
	{		
		while(!q1.isEmpty())
		{
			node n=q1.remove();
			if(n.left == null && n.right == null)
				return depth;
			q2.add(n.left);
			q2.add(n.right);
		}
		depth++;
		while(!q2.isEmpty())
		{
			node n=q2.remove();
			if(n.left == null && n.right == null)
				return depth;			
			q1.add(n.left);
			q1.add(n.right);
		}
		
	}
}

- gulusworld1989 October 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@gulusworld1989

I liked the approach. But there seem to be some incompleteness.

Consider a tree with root node a, its right node b and b's left node C. So in the case, depth should be 2 but your algo returns 1.

Can you please check?

also please explain your logic so that I want to get clarification on this.

- SS June 06, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

They are essentially the same complexity-wise. Recursive approach implicitly does DFS building a stack, and the other builds queue.

- memo October 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int depthWithRec(Node * node)
{
if(node == null)
return null;
else
return min(depth(node->left), depth(node->right) + 1);
}

int depthWithoutRec(Node * node){
if(node != null) {
int lDepth = 0, rDepth = 0;
queue.enqueue(node);
while(queue()! = empty()) {
node = dequeue();

if(node->left != null) {
lDepth++;
queue.enqueue(node);

}
if(node->right!= null) {
rDepth++;
queue.enqueue(node);
}
return(min(lDepth,rDepth) +1);
}
}
else
return 0;
}

- Richa Aggarwal October 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think this will not work. Please try to trace the program over an input.

- Anonymous May 01, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int mindepth(struct node* root)
     {
           if(root == NULL)
              return 0;
          else
          {
                int lefttreedepth = mindepth(root->left);
                int righttreedepth = mindepth(root->right);
                if(lefttreedepth<righttreedepth)
                     return lefttreedepth+1;
               else 
                      return righttreedepth+1;
          }
}

- Rad November 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think so ur code may even count nodes with one child as leaves. A slight modification might help..

int mindepth (struct node *root)
{
    if (root == NULL)
        return 0;
    else
    {
        int lefttreedepth = mindepth (root->left);
        int righttreedepth = mindepth (root->right);
        if (lefttreedepth < righttreedepth)
        {
            if (lefttreedepth == NULL)
                return righttreedepth + 1;
            else
                return leftreedepth + 1;
        }
        else
        {
            if (righttreedepth == NULL)
                return leftreedepth + 1;
            else
                return righttreedepth + 1;
        }
    }

}

- Siraj January 05, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Depth(Node node)
{
  if(node == null) return 0;
  else return 1 + min(Depth(node->left),Depth(node->right));

}

Since it will traverse all the nodes I think complexity is O(n), but if you follow a dfs or an iterative approch complexity will be reduced, correct me if I am wrong

- Uday March 02, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry its bfs not dfs :)

- Uday March 02, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about BFS counting the number of nodes in each layer interatively, until find the layer in depth d has nodes fewer than 2^(d-1)

- Anonymous March 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//Recursive is better
public int minDepth(Node root)
{
if (root == null) { return 0; }
return 1 + Math.Min(minDepth(root.leftNode), minDepth(root.rightNode));
}

- anup.h.nair December 31, 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