Microsoft Interview Question for Developer Program Engineers


Country: India
Interview Type: Written Test




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

bool IsSum (root, sum) {
if(sum == 0)
return true;

else if (root == null)
return false;

else
return (IsSum(root->left, sum - root->val) ||
IsSum(root->right, sum - root->val) )
}

- pal October 31, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Small modification:

if ((sum == 0) && (root == null))
return true;

else if (root == null)
return false;

else
return (IsSum(root->left, sum - root->val) ||
IsSum(root->right, sum - root->val) )
}

- pal October 31, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This has exponential time complexity

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

exponential ?? How-come ?

- pal October 31, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

> This has exponential time complexity

No, it doesn't. There are no loops, and one call per node.

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

@pal :
think ur code requires little modification ..


consider the below tree

4
            /   \
           6     8
            \   / 
             5 1

required sum is 10 .. such path doesnt not exist , but ur program returns true ..

- msankith October 31, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, it has exponential complexity. For each function call you have two function calls.

Hence its complexity is O(2^n).

- Nani November 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No. Its making only one one visit per node.

- Anonymous November 04, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

> Yes, it has exponential complexity. For each function call you have two function calls.
Hence its complexity is O(2^n).

You're wrong about the n. Your 'n' is the tree Height, which is log(n). Thus, your '2^n' is actually 2^log(n) = n

- avico81 October 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

@msankith

yeah ... you are right ..

I guess this will do ..

bool IsSum (root, sum) {
if(root == null)
return;

if ((sum == root->val) && (root->left == null) && (root->right == null)) //reached root and sum is found
return true;

else if ((root->left == null) && (root->right == null)) // reached leaf node but sum not found
return false;

else
return (IsSum(root->left, sum - root->val) ||
IsSum(root->right, sum - root->val) )
}

- pal October 31, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Again one modifcation,

if(root == null)
return *false*;

- pal October 31, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@pal : another modification

if ((sum == root->val) && (root->left == null) && (root->right == null)) //reached root and sum is found
return true;

should be

if ((sum == 0) && (root->left == null) && (root->right == null)) //reached root and sum is found
return true;

- SegFault December 10, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@pal : another modification

if ((sum == root->val) && (root->left == null) && (root->right == null)) //reached root and sum is found
return true;

should be

if ((sum == 0) && (root->left == null) && (root->right == null)) //reached root and sum is found
return true;

- SegFault December 10, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Are there some more constraints, say on the values?

- eugene.yarovoi October 31, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

No

- msankith October 31, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about this ?

/* check if leaf node exists and sum from root to given leaf node = given sum */
	public boolean checkLeafExistsAndSumFromRootIsGivenSum (Object k, int checkSum) throws NoRootException {
		
		/* check if root is present */
		if(getRoot()==null) throw new NoRootException();
		
		/* boolean present to return */
		boolean isPresent = false;
		
		/*sum to sum all items */
		int sum=0;
		
		/*node to store root */
		BinarySearchTreeNodeList node = getRoot();
		
 		/* loop untill null is reached */
		while(node!=null){
			
			/* compare item with object to search */
			int c = (((Comparable)k).compareTo(node.getItem()));
			
			/* if k > node.item */
			if(c>0){
/* if node.right exists then move right else no node exists; return false and stop */
				if(node.getRight()!=null){
		sum=sum (Integer)node.getItem();					
					node=node.getRight();
					isPresent=true;
				}else{
					isPresent=false;break;
				}
				
			}else if(c<0){ /* if k < node.item */
/* if node.left exists move left else no node exists;return false and stop; */
				if(node.getLeft()!=null){
					sum=sum+(Integer)node.getItem();
 					node=node.getLeft();
					isPresent=true;
				}else{
					isPresent=false;break;
				}
			}else if(c==0){
/* if k==node.item then check if sum== check sum else return false and stop; */
				sum=sum+(Integer)node.getItem();
  				if(sum==checkSum)
					isPresent=true;
				else isPresent=false;
				break;
				 
			}
		}
		return isPresent;	
	}

- sreeprasad.sp November 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool isPathExists(node *node,int sum)
{
if(node == null)
return false;
sum = sum - node->value;
if(sum == 0)
return true;
else
if(sum < 0 )
return false;

return (isPathExists(node->left,sum) || isPathExists(node->right,sum))

}

- jagadish.dande November 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

D(n, k)
	if(k == 0 && n == NULL)
		return true
	if(k!=0 && n == NULL)
		return false
	if(k < 0)
		return false
	return D(n->l, k - n->l->d) ||	D(n->r, k - n->r->d)

- Prateek Caire November 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Prateek: there is little modification required in this algo as mentioned by msankith in first answer's comment [mentioned with tree diagram].
Second answer to this question is correct.

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

Elegantly done using DP:
maintain OPT( node, k ) for each node such that OPT( node, k ) = true if some path starting from this node has the sum k. compute from Bottom up from leaf upwards
i.e. OPT( node, k ) = true if OPT( node->left, k - node->value ) = true || OPT( node->right, k - node->value ) = true. O(nk)

- subramanian.ganapathy86 January 05, 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