Adobe Interview Question for Computer Scientists






Comment hidden because of low score. Click to expand.
3
of 5 vote

i think ans is 27....

(5,12)
=(5,6)*(5,6)
=(5,3)*(5,3)*(5,3)*(5,3)
={5*(5,1)*(5,1)}*{5*(5,1)*(5,1)}*{5*(5,1)*(5,1)}*{5*(5,1)*(5,1)}
=[5*{(5*(5,0)*(5,0))*(5*(5,0)*(5,0))}]*[5*{(5*(5,0)*(5,0))*(5*(5,0)*(5,0))}]*[5*{(5*(5,0)*(5,0))*(5*(5,0)*(5,0))}]*[5*{(5*(5,0)*(5,0))*(5*(5,0)*(5,0))}]

@Sandeep think 10 times before saying anybody as FOOL :P

- srirupesh@gmail.com July 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

difference between sandeep's code and question's pow code is 
else if(n==1) return x;
sandeep's code will give 11 as answer but correct answer for this question is 27 as explained by srirupesh.

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

In the above solution, the complexity will be linear. Slight modification below to make it logarithmic

int pow(int x, int n)
{
if(n==0)return(1);
else if(n%2==0)
{
int tmp = pow(x,n/2);
return tmp*tmp;
}
else
{
return(x*pow(x,n-1));
}
}

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

You are ryt about your implementation. But the question is something else.
I think the correct answer is 27.

- Gaurav June 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

oh...sorry...i misunderstood the question...nd yes the answer should be 27

- Anonymous June 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The answer for the 2nd solution should be 5. Am I correct ?

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

@gaurav: can u explain how counts is coming 27

- Amit June 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

how the answer is 27...i am getting 13

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

#include<stdio.h>
#include<conio.h>

int count;

int pow(int x, int n)
{
if(n==0)return(1);
else if(n%2==0)
{
count++;
return(pow(x,n/2)*pow(x,(n/2)));

}
else
{
count+=2;
return(x*pow(x,n/2)*pow(x,(n/2)));
}
}



int main()
{
count = 0;
pow(5,12);
printf("\nMultiplication Count : %d", count);
getch();
return 0;
}

Run to see the answer..:)

- Daddy's Home July 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

below is the java code which gives the count as 11 and which is correct 27 is really foolish.

public static void main(String[] args)
{


new TestMethods().pow(5, 12);
System.out.println("\nMultiplication Count :"+ count);

}
private static int count=0;
public int pow(int x, int n)
{
if(n==0)return(1);
else if(n==1) return x;
else if(n%2==0)
{
count++;
System.out.println("\nMultiplication Count even:"+ count+" "+x+" "+n);
return(pow(x,n/2)*pow(x,(n/2)));

}
else
{
count+=2;
System.out.println("\nMultiplication Count :odd"+ count+" "+x+" "+n);
return(x*pow(x,n/2)*pow(x,(n/2)));
}
}
if you have common mathametic sence you can undersatnd that 5^12 or pow(5,12) sould have exactly 11 multiplications

- Sandeep July 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Original code:
5^12 = 5^6 * 5^6
5^6 = 5^3 * 5^3
5^3 = 5 * 5^2
5^2 = 5^1 * 5^1
5^1 = 5 * 5^0

5 multiplication

unsigned int power(unsigned int x, unsigned int y)
{
unsigned int prod = 1, temp = x;
assert(x || y);
if (x < 2) return x;
for (; y; y = y >> 1)
{
if (y & 1) prod *= temp;
temp = temp * temp;
}
return prod;
}

here has no more than 2 * floor(log2(y)) multiplication. In the case, y = 12, it needs 6

- GhostArcher September 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

sorry 5 for this one. The original use much more:

int pow(int x, int n)
{
if(n==0)return(1);
else if(n%2==0)
{
int tmp = pow(x,n/2);
return tmp*tmp;
}
else
{
return(x*pow(x,n-1));
}
}


Orignal Code:

1 + 2 * ( 1 + 2 * ( 2 + 2 * (2)))

Every time it recursive call, the number of underlining multiplicatoin duplicate.

- GhostArcher September 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Multiplications would be 6

- alexander July 04, 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