Interview Question


Country: India




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

Summation of (3^(i/2) * 4^(i-1)/2) ,where i runs from 1 to k.

Here i/2 and (i-1)/2 are the integral values of quotients.

- Murali Mohan July 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

+1. I am wondering if there is a formula to compute this series for a O(1) solution.

- oOZz July 14, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good one.... +1 from my side too..

- Rahul August 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

below I have posted O(1) solution

- algos August 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

O(1) solution

	number of children at even level = 4
	number of children at odd level  = 3
	
	x = 1 + 4;
	y = 4 * 3;

	int n = (level-1)/2 - 1;
	double res;

	if(level % 2)
		res = (pow(y, n+1)*(x+y-1)-x) / (y-1);
	else
		res = x + (x*y*(pow(y, n+1)-1)) / (y-1);

	cout << res << endl;


below is proof

number of nodes	      level
1			               1
4			               2
4*3		   	               3
4*3*4			      4
4*3*4*3			      5
4*3*4*3*4		      6
4*3*4*3*4*3		      7
4*3*4*3*4*3*4	      8

lets take odd level at 7
1 + 4 + 4*3 + 4*3*4 + 4*3*4*3 + 4*3*4*3*4 + 4*3*4*3*4*3
1 + 4 + 4*3 ( 1 + 4 + 4*3 + 4*3*4 + (4*3)^2)
1 + 4 + 4*3 ( 1 + 4 + 4*3 (1 + 4 + (4*3))

lets take x = 1+4 and y = 4*3

this equation looks like 

x + y (x + y (x + y))
x + y (x + xy + y^2)
x + xy + xy^2 + y^3
x (1 + y + y^2) + y^3
...
this is summation series x ( 1+ y + y^2 + y^3+ y^4....+y^n) + y^(n+1) 

summation for 1+ y + y^2 + y^3+ y^4....+y^n = (y^(n+1) - 1) / (y-1)

answer for odd levels is  x(y^(n+1)-1)/(y-1) + y^(n+1)

for even level it will be

x + xy + xy^2 + xy^3 + xy^4...
x + xy(1 + y + y^2 + y^3 + ...)

after solving the summation series for even levels is

x + xy(y^(n+1)-1)/(y-1)

- algos July 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice !! :)

- alex August 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

simple DP problem

Let n[1...k] be the array of node_count at level i where 1 < = i <= k. We need not go beyond k as all the leaves are at level k

So,the desired result is n[1] +....+n[k]

The recurrence relation in the DP is

n[i+1] = 3 * n[i] if i %2==1,
       = 4 * n[i] , otherwise

Code :
   n[1] = 1 //  this represents the root node 
   numNodes = n[1];
   for( i = 2 : i <= k ; i++){
          if((i-1) %2 ==1){
                      n[i] = 3 * n[i-1];
		}
           else {
                       n[i] = 4 * c=n[i-1];
                }
            numNodes += n[i];
	}
      print(numNodes);

- random July 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It's simple mathematics , You can solve it in O(1):

int number_of_nodes(int k ){
	int result = 0;
	if (k % 2 == 0){
		result = (Math.pow(12 , k/2 - 1) - 1) / 11  + 3 * ( Math.pow(12, (k-1)/2) -1) /11 ;
	}else{
		result = (Math.pow(12,(k-1)/2) - 1) / 11 + 3 * (Math.pow(12,(k-1)/2 - 1 ) - 1 )/ 11;
	}
}

- Arian July 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The got the formula wrong, in addition, the code does not even compile. Try to at least run the code once before posting.

- oOZz July 14, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is geometric progression problem. Can resolve it by doing odd and even levels separately and sum them. Someone may be able to improve this.

In summary, the geometric progression for odd levels is
a=(4.3.4.3) = 144
r=4.3 = 12
Valid for k>=5.
The normal equation for geometric progression is
a*(1-r^m)/(1-r) where m is the number of terms in the series. Since 1st term is at k=5, 2nd term is at k=7, etc, we can get the Nth term using k/2-1.
a*[1-r^(k/2-1)]/(1-r)

Similarly, solving for even levels
a=3
r=(4.3)=12
Valid for k>=2
a*[1-r^(k/2)]/(1-r)

The Java code is given below.

public class SumNodes
{
    public static void sumNodes(int k) {
        int total = 0;
        if(k%2==0) { // even
            total = sumEvenLevel(k);
            total += sumOddLevel(k-1);
        }
        else {
            total = sumOddLevel(k);
            total += sumEvenLevel(k-1);
        }
        System.out.println("Total Nodes in tree with level " + k + ": " + total);
    }

    public static int sumEvenLevel(int k) {
        int retval = 0;
        int a = 3;
        if(k>=2) {
            retval = a * (1 - (int)Math.pow(12,k/2))/(-11);
        }

        return retval;
    }
    
    public static int sumOddLevel(int k) {
        if(k==1) return 1;
        if(k==3) return 13;
        int retval = 13;
        int a = 144;
        if(k>=5) {
            retval += a * (1-(int)Math.pow(12,k/2-1))/(-11);
        }
        return retval;
    }
    
    public static void main(String[] args) {
        sumNodes(Integer.parseInt(args[0]));
    }
}

- edlai July 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Another solution with pairs as geometric progression terms.
Level 1 and Level 2 = 1st term
Level 3 and Level 4 = 2nd term

a=(1+3) = 4
r=(3.4) = 12
This is true for k>=1

public static void sumNodes_1(int k) {
        int total = 0;
        if(k%2==0) { // even
            total = 4 * (1-(int)Math.pow(12,k/2))/(-11);
        }
        else {
            total = 4 * (1-(int)Math.pow(12,(k-1)/2))/(-11) + (int)(Math.pow(3,k/2)*Math.pow(4,k/2));
        }

        System.out.println("SumNodes_1 Level " + k + ": " + total);
    }

- edlai July 16, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

There should be a mistake in the phrasing of this problem. A full tree by definition is a binary tree whose nodes have (strictly) either two (i.e., non-leaves) children or none(i.e., leaf-nodes).

- Anonymous July 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Read the question carefully. It says:
>>consider a full tree. .Every node at odd level...

there is no mention of the tree being binary.

- Anonymous July 24, 2013 | 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