Morgan Stanley Interview Question for Sales Development Representatives


Country: India




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

package interview_questions;

/**
*
* @author just_do_it
*
*/
public class CountBinaryHeaps {


/*
* f(38) = 37C22 * f(15) * f(22)
*/
static long numBinaryHeaps(int n){
if(n == 1 || n == 2) {
return 1;
} else if (n == 3){
return 2;
}

int numElementsChild1, numElementsChild2;
int n1 = largestPowerOf2LessOrEqual(n);
if(n1 - 1 + n1/2 >= n){
numElementsChild1 = n1/2 - 1;
numElementsChild2 = n - n1/2;
} else {
numElementsChild1 = n1 -1;
numElementsChild2 = n - n1;
}

long res = choose(n-1,numElementsChild1) * numBinaryHeaps(numElementsChild1) * numBinaryHeaps(numElementsChild2);
return res;
}

public static int choose(int n, int m){
if(n < m)
return 0;
if(m == 0 || n == m)
return 1;
return choose(n-1,m-1)+choose(n-1,m);
}

static int largestPowerOf2LessOrEqual(int n){
int pow2 = 1;

while(pow2*2 < n){
pow2 *= 2;
}
return pow2;
}

public static void main(String[] args){

int[] testcases = {1 , 2 , 3 , 4 , 5 , 6};
long[] results = {1 , 1 , 2 , 3 , 4*2, 10*2 };
for(int i = 0; i < testcases.length; i++){
int n = testcases[i];
long res = numBinaryHeaps(n);
System.out.println(n + "\t" + res + "\t" + results[i]);
}
}
}

- just_do_it July 18, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package interview_questions;

/**
 * 
 * @author just_do_it
 *
 */
public class CountBinaryHeaps {

	
	/*
	 * f(38) = 37C22 * f(15) * f(22)
	 */
	static long numBinaryHeaps(int n){
		if(n == 1 || n == 2) {
			return 1;
		} else if (n == 3){
			return 2;
		}
		
		int numElementsChild1, numElementsChild2;
		int n1 = largestPowerOf2LessOrEqual(n);
		if(n1 - 1 + n1/2 >= n){
			numElementsChild1 = n1/2 - 1;
			numElementsChild2 = n - n1/2;
		} else {
			numElementsChild1 = n1 -1;
			numElementsChild2 = n - n1;
		}
		
		long res = choose(n-1,numElementsChild1) * numBinaryHeaps(numElementsChild1) * numBinaryHeaps(numElementsChild2);
		return res;
	}
	
	public static int choose(int n, int m){
	    if(n < m)
	        return 0;
	    if(m == 0 || n == m)
	        return 1;
	    return choose(n-1,m-1)+choose(n-1,m);
	}
	
	static int largestPowerOf2LessOrEqual(int n){
		int pow2 = 1;
		
		while(pow2*2 < n){
			pow2 *= 2;
		}
		return pow2;
	}
	
	public static void main(String[] args){
		   
		   int[] testcases = {1 , 2 , 3 , 4 , 5 ,  6};
		   long[] results = {1 , 1 , 2 , 3 , 4*2, 10*2  };
		   for(int i = 0; i < testcases.length; i++){
			   int n = testcases[i];
			   long res = numBinaryHeaps(n);
			   System.out.println(n + "\t" + res + "\t" + results[i]);
		   }
	   }
}

- just_do_it July 18, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Care to explain a bit ? Occassional comments help

- R July 20, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

i think it is catalan number and is (n+1)*( 2n choose n)

- tango July 25, 2014 | 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