Microsoft Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

int countTrees(int numKeys) { 

  if (numKeys <=1) { 
    return(1); 
  } 
  else { 
    // there will be one value at the root, with whatever remains 
    // on the left and right each forming their own subtrees. 
    // Iterate through all the values that could be the root... 
    int sum = 0; 
    int left, right, root; 

    for (root=1; root<=numKeys; root++) { 
      left = countTrees(root - 1); 
      right = countTrees(numKeys - root); 

      // number of possible trees with this root == left*right 
      sum += left*right; 
    } 

    return(sum); 
  } 
}

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

Good solution

- IntwPrep.MS December 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The problem can be solved by finding the Catalan number for the value of n...

The formula for finding the Catalan number is (1/(n+1))*(2n C n)... where C is combination...

Using this formula we have, if n = 4, no of BST's possible is 14.
If n = 5, no of BST's possible is 42 and so on...

- Rahul

- Catalan numbers... October 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The number of non-isomorphic ordered binary trees with 5 vertices. 42 as Rahul said.

- xbai@smu.edu October 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Its catalan number, The same question was asked to me in an Google interview

- Swap October 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int countTrees(int numKeys) { 

  if (numKeys <=1) { 
    return(1); 
  } 
  else { 
    // there will be one value at the root, with whatever remains 
    // on the left and right each forming their own subtrees. 
    // Iterate through all the values that could be the root... 
    int sum = 0; 
    int left, right, root; 

    for (root=1; root<=numKeys; root++) { 
      left = countTrees(root - 1); 
      right = countTrees(numKeys - root); 

      // number of possible trees with this root == left*right 
      sum += left*right; 
    } 

    return(sum); 
  } 
}

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

this algorithm will not return the right information

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

Can be solved, by first calculating for tree with 1 node, then tree with 2 nodes, then tree with 3 nodes, so on so forth. Like for tree with 3 nodes it can be calculated by assuming first node as root node so there will be 0 node on left side and 2 nodes on right side, then 1 on left and 1 on right , other being 2 nodes on left side and 0 node on right side and adding all.

- ishantagarwal1986 October 24, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

NT(i, j)
	if(i == j)
		return 1
	for each k from i to j
		v = v+ NT(i, k-1)*NT(k+1, j)
	return v

main
	NT(1, n)

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

This can be much improved. Looks like a classic dynamic programming problem. Just have a hashmap/grid to memoize the values and blurt them out if the # of trees for that value is already calculated.

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

public static void main(String[] args) {
		System.out.println(noOfBSTs(5));
	}
	
	public static HashMap<Integer, Integer> map = 
			new HashMap<Integer, Integer>();
	
	public static int noOfBSTs(int n){
		if(n < 3) return Math.max(0, n);
		Integer cachedResult = map.get(n);
		if(cachedResult != null)
			return cachedResult.intValue();
		int sum = 0;
		for(int root = 0; root < n; root++){
			sum += noOfBSTs(root - 1);
			sum += noOfBSTs(n -1 -root);
		}
		map.put(n, sum);
		return sum;
	}

This code gave me result of 24 for input: 5
Did I do a mistake or this is not actually Catalan Numbers problem?

Regards

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

its: 2^n - n

- Nascent Coder October 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

If you don't surr, need not sumbit since already a good solution is posted by Rahul then what is need of submitting wrong ans???

- Anonymous November 10, 2011 | 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