Interview Question


Country: United States




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

public static int catalanNumber(int n) {
		if (n == 1||n==0)
			return 1;
		if (n == 2)
			return 2;
		int sum = 0;
		for (int i = 0; i < n; i++)
			sum = sum + catalanNumber(i) * catalanNumber(n - i - 1);
		return sum;
	}

- salvo4u June 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You really need to be using dynamic programming for the recursion here, unless your inputs are very very small. Otherwise the runtime is wildly exponential.

- eugene.yarovoi June 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yeah i get ur point eugene
Recursion could be a real mess here ........

- salvo4u June 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

With DP:

int catalanNum(int n)
{

	T[0]=1;
	T[1]=1;
	T[2]=2;
	
	for(int i=3;i<n;++)
	{
		int sum=0;
		for(int j=0;j<i;j++)
		{
			sum=sum+T[j]*T[n-i-1];
		}
		T[i]=sum;
	}
	return T[n-1];
}

- Ashay July 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry correction:
sum=sum+T[j]*T[n-j-1];

- Ashay July 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

@Ram :
* Assume u have n nodes
* the left subtree has i nodes
* the right subtree will have n-i-1 nodes
Let
T(i) the number of trees for the left subtree then
T(n-i-1) will the number of subtrees for the right

Total number of trees will be T(i)*T(n-i-1) for all i belongin to 0 to n-1

The recurssion is what i have coded above wothout DP

- salvo4u June 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Ok.. got it :) Thanks

- Ram June 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Without using catalan number :
Let A[] contain the nodes in sorted order.

int numberOfTrees(int[] A, int start, int end) {
int count;
if (start == end) return 0; //0 nodes
if (start+1 == end) return 1; //1 node
if (start+2 == end) return 2; //2 nodes
for (int i = start; i < end; i++) {
int left += numberOfTrees(A, 0, start);
int right += numberOfTrees(A,start+1, A.length);
count += left*right;
}
return count;
}

number of trees can be known by calling numberOfTrees(A,0,A.length)

The array has been taken in sorted order, so that the binary search trees are formed, which will be different in all the cases, and hence there would be no overlapping of structurally same binary trees and at the same time, take into

- Achilles June 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

catalan number,you can calculate use dynamic programming

- Anonymous June 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

uses DP :

static HashMap<Integer, Integer> hmap=new HashMap<Integer,Integer>();
	public static int catalanNumber(int n) {
		if (n == 1||n==0)
			return 1;
		if (n == 2)
			return 2;
		if(hmap.containsKey(n))
			return hmap.get(n);
		int sum = 0;
		for (int i = 0; i < n; i++)
			sum = sum + catalanNumber(i) * catalanNumber(n - i - 1);
		hmap.put(n,sum);
		return sum;
	}

- salvo4u June 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You could have just used an array to keep track of what you've calculated already, since getting the n-th value requires knowing (and thus storing) precisely all the values from catalan(0) to catalan(n-1).

- eugene.yarovoi June 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

How is catalan numbers a solution?

Wiki says catalan gives the number of "full" binary trees. The set of "full" binary trees with n nodes is a subset of all structurally different binary trees.

- axecapone June 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

USING DP

int difftrees(int n)
{

int arr[n+1];
arr[0]=1;
arr[1]=1;
arr[2]=2;

int i,j;
	for(i=3;i<=n;i++)
		{
			arr[i]=0;		
			for(j=0;j<=i-1;j++)
			arr[i]+= (arr[j]*arr[i-1-j] );
		}

return arr[n];

}

- alexander June 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

const int CatalanNos[12]={1,1,2,0};
long int catalan(int n){
if(n == 0 || n == 1)
return 1;
if(n == 2)
return 2;
if(CatalanNos[n] == 0)
{
long int sum = 0;
for (int i = 0; i < n; i++)
sum = sum + catalan(i) * catalan(n - i - 1);
return sum;
} else {
return CatalanNos[n];
}
}

- SantiagoYemmiganur June 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Can you please explain it ? And without catalan no how can we do it ?

- Ram June 10, 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