LexisNexis Interview Question for Software Engineer / Developers






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

Breadth First Search

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

Algorithm:
If node is null, return 0.
Else find min of the minimum depth for left node and right node + 1.

int minDepth(TreeNode n)
{
 if ( n == null )
  return 0;
 return Math.min( minDepth(n.left), minDepth(n.right) ) + 1;

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

This will not work. It will return 2 for the example given. Try it yourself.

This could work

int MinDepth(TreeNode *node)
{
  if( node == 0 )
  {
    return 0;
  }
  int hL = MinDepth(node->left);
  int hR = MinDepth(node->right);
  if( (hL == 0) || (hR == 0) )
  {
    return max( hL, hR ) + 1;
  }
  return min( hL, hR ) + 1;
}

- riderchap November 20, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@riderchap. Excellent! I've seen wrong implementations of this problem many times. A small fix: subtract 1 from the final result. Also, there is no need to traverse the whole tree to find out the minDepth. You can traverse the tree level by level. The level where you find the first leaf is your minDepth.

- Jaime January 22, 2013 | 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