Interview Question for Software Engineer / Developers


Country: India
Interview Type: Phone Interview




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

int findDiff(root)
{
	if(!root)
		return 0;
	l=0;r=0;
	l=findDiff(root->left);
	r=findDiff(root->right);

	root->val=l-r;
	return l+r+1;
}

Please let me know if anything is missing.

- Aashish July 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sorry it should be total number of nodes in the left sub-tree..

- raj.kamal.nitj July 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sorry it should be total number of nodes in the left sub-tree..

- raj.kamal.nitj July 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

fun(Node n) {
      int lval = fun(n.left), rval = fun(n.right);
      n.value = lval - rval;
      return lval + rval;
}

// Node definition can be derived from above code.

- Anonymous July 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

// Looks like I was logged in. So posting again.

fun(Node n) {
      if (n == null) return 0;
      int lval = fun(n.left), rval = fun(n.right);
      n.value = lval - rval;
      return lval + rval;
}

- ashwini verma July 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You forgot to think about the following case

1. Left leaf has right node
2. Right leaf has left node

- TS July 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assume Node has (int) data as member:

public int LeftMinusRight(Node root)
{
return Sum(root.Left) - Sum(root.Right);
}

private int Sum(Node node)
{
return Sum(node.Left) + Sum(node.Right);
}

- TS July 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

private int Sum(Node node)
{
return Sum(node.Left) + Sum(node.Right);
}

is wrong

return node.Data + Sum(node.Left) + Sum(node.Right);

is correct

- TS July 05, 2012 | 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