Adobe Interview Question for Development Support Engineers






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

What are the allowed functions/operations?
You can do power with only plus (so 0 multiplication)

- ftfish July 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

if you wanna give an O(n^k) solution

- David July 17, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I just want to make sure that the problem is not meaningless.
E.g. if divisions are allowed, it's possible to do better than the trivial O(logn) divide-and-conquer solution.

- ftfish July 17, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

double power(double x, unsigned int n)
{
   if (!n) return 1.0;
   if (n == 1) return x;
   double result = power(x, n/2);
   result *= result;
   return (n&1) ? result*x : result;
}

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

// forgot to login :)
double power(double x, unsigned int n)
{
   if (!n) return 1.0;
   if (n == 1) return x;
   double result = power(x, n/2);
   result *= result;
   return (n&1) ? result*x : result;
}

- fiddler.g July 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice solution :)

- XYZ July 17, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

In the best case when n is a power of 2, we need to use log(n) times, otherwise (the worst case) 2*log(n).

- fiddler.g July 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

type y=1;

while( N )
{
   if( N&1 )
     y*=X;

   X*=X;
   N>>=1;

}

return y;

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

The following solution computes it using floor(log(n)) multiplications.

double power(double x, unsigned int n) {
double res = 1.0;
double m = x;

if (x == 0.0)
return 0.0;

while (n) {
if (n & 1)
res *= m;

m *= m;
n >>= 1;
}

return res;
}

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

the guy above posted almost the same solution as mine. i believe our solution is the best in terms of the number of multiplications.

- Anonymous July 17, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

i think if n is odd, the number of multiplications would be more then log(n)..

- XYZ July 18, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

ya..the code is incorrect.. i just tested. gives me same answer for : 2^4, 2^5, 2^6, 2^7 = 256

- chenna September 29, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

if n is a power of 2 then it will take logn+1
if not it will take max 2*(logn)+1
if u add one statement within if that is
if(n==1)
break;

it reduces one more multiplication
because we are trying to reduce number of multiplications

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

double powerofN(double num, unsigned int power)
{
double reminder=1;
if( power < 1)
return 1;
while(power > 1)
{
if(power%2 ==0 )
{
num = num * num;
power = power/2;
}
else
{
reminder = reminder * num;
num = num * num;
power = (power - 1)/2;
}
}
num = num * reminder;
// printf("The value of num is %f and result is %f\n", num, result);
return num;
}

- shahidnitt August 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You mean remainder!lol

- bitch February 11, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <vector>
#include <math.h>
#include <iostream>

using namespace std;

int main(){

	int x = 2;
	int n = 15;

	//compute pow(x,n)
	//In this case the algorithm computes in atmost ceil(2 * (logn base2))+1 computations
	vector <long> arr;
	arr.push_back(x);
	int baseComputs,numOfComputs = 0;
	long result = 1;
	
	baseComputs = floor(log2(n));
	int i=1;
	for (;i<=baseComputs;i++){
		arr.push_back(arr[i-1]*arr[i-1]);numOfComputs++;
		if(n&(1<<i-1)){
			result=result*arr[i-1];numOfComputs++;
		}
	}
	if(n&(1<<i-1)){
		result=result*arr[i-1];numOfComputs++;
	}

	cout << "pow("<<x<<","<<n<<") is :: "<<result<<". with "<<numOfComputs<<" multiplications"<<endl;
	return 1;
}

- chenna September 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="" line="1" title="CodeMonkey73107" class="run-this">public class Power {

public static int power(int base, int power) {
int res = 1;
if (power > 0) {
int mask = 0x1;
while (mask <= power) {
if ((mask & power) != 0) {
res *= base;
}
mask <<= 1;
base *= base;
}
}
return res;
}

public static void main(String[] args) {
System.out.println(Power.power(3, 6));
System.out.println(Power.power(5, 4));
System.out.println(Power.power(6, 4));
System.out.println(Power.power(10, 7));
}
}
</pre>

- Anonymous March 29, 2011 | 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