Yahoo Interview Question for Software Engineer / Developers






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

Seriously I dont understand why people have got various answers for same problem.

Binary Tree : 2^n - n
BST : (2n)C(n)/(n+1) i.e catalan number which is same as (2n)!/( (n!)*(n+1)! )

- Saurabh Kr Vats June 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Binary Tree : 2^n - n

I think this is not correct.
If n=2, your ans is 2, but I think it should be 4:
1 1 2 2
/ \ / \
2 2 1 1

- gaoxiao October 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

6 binary trees, 1 binary search tree

- chennavarri October 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

WRONg!!

- Aditya October 12, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Great. Why don't u post ur answer

- Chenna October 12, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

6 binary trees, 3 binary search tree

- mitstud October 21, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

30 bt and 5 bst!

- moti February 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

binary tree-> (2^n)-n
BST-> (2n)C(n)/(n+1) its catalan number

- Anonymous October 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

binary tree-> (2^n)-n
BST-> (2n)C(n)/(n+1) its catalan number

- sarvesh October 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 3 votes

i spent lot of time on this question and ans is

# of BST - (2n)C(n) /(n+1) -- catalan number

# of BT - catalan number * n! = 2n ! / (n+1) !

- sravan November 08, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Both will be same. In binary tree the layout matters, but the position doesn't. 1-2-3 is same as 2-1-3. In BST position matters, but the structure is constrained.

- ibakar August 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

According to how I'm understanding the question, it's actually 5 distinct BSTs and 5*3! = 30 distinct binary trees. It all depends on what you count as a distinct tree though.

The reasoning is this: there are 5 ways to arrange the nodes, if you count a tree like

x
    /   \
  x     x

to be distinct from

x
  \
   x
    \
     x

(The differing view could be is that they're both a line, in which case there's only 1 way to arrange 3 nodes, as every configuration is reducible to a line for n=3. But let's not go with this interpretation of the problem.)

These 5 distinct trees can have any permutation of 1, 2, and 3 assigned to them if it's just a binary tree. If it's a binary search tree, the values of the nodes are required to be in sorted order when in-order traversal is done, which means that for each tree shape, there's only one possible configuration of the values of the nodes (the i-th node in the in-order traversal must have the value i).

Therefore, I take the answer to be 5 for BSTs and 5*3! =30 for arbitrary binary trees.

- eugene.yarovoi September 14, 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