Salesforce Interview Question for Developer Program Engineers


Country: India
Interview Type: Written Test




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

if n is even then power(x, n) = power(x, n / 2) * power(x, n / 2)
else power(x, n) = x * power(x, n - 1)

Easiest recursive implementations is

int power(int x, int n) {
    if (n == 1) return 1;
    else if (n % 2 == 1) return x * power(x, n - 1);
    else {
        int b = power(x, n / 2);
        return b * b;
    }
}

- Mike S January 17, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This has an error. It should be:

int power(int x, int n) {
if (n == 1) return x; // <------ instead of 1, it should be x
else if (n % 2 == 1) return x * power(x, n - 1);
else {
int b = power(x, n / 2);
return b * b;
}
}

- Anonymous March 27, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

double Power(double x, int n)
{
	if (n == 0)
	{
		return 1;
	}

	double result = Power(x, n / 2);
	if (n % 2 == 0)
	{
		return result * result;
	}
	else
	{
		return result * result * x;
	}
}

double Pow(double x, int n)
{
	if (n < 0)
	{
		return 1 / Power(x, -n);
	}
	else
	{
		return Power(x, n);
	}
}

- Anonymous January 18, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This program runs faster, and also uses DP for those who use it.

public class power {

	int pow[];
	
	void init(int p) {
		pow = new int[p];
	}
	
	int powers(int n, int p) {
		if(p==1)
			return n;
		
		if(pow[p/2]==0) 
			pow[p/2] = powers(n,p/2);
		
		return pow[p/2] * pow[p/2];
		
	}
	
	public static void main (String args[]) {
		
		Scanner s = new Scanner (System.in);
		int n = s.nextInt(), p=s.nextInt();
		
		power obj = new power();
		obj.init(p);
		System.out.println((p%2==0)?obj.powers(n,p):n*obj.powers(n,p-1));
		
	}
}

- Anindya Dutta January 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static double power(int x , int n){
		if(n==0) return 1;
		if(n == 1) return x;
		if(n%2 == 0)
			return power(x,n/2)*power(x,n/2);
		return x*power(x,n/2)*power(x,n/2);
	}
	
	public static void main (String args[]){
		System.out.printf("%.0f", power(20,9));
	}

- Sravn October 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Power{

	public double power(int number, int n){
		if(n < 0){
			return 1 / powerUtil(number, n * -1);
		}else if(n == 0){
			return 1;
		}else{
			return powerUtil(number, n);
		}
	}

	private double powerUtil(int number, int n){
		if(n == 1){
			return number;
		}else if(n % 2 == 0){
			return powerUtil(number * number, n / 2);
		}else{
			return number * powerUtil(number * number, n / 2);
		}
	}

	public static void main(String[] args) {
		Power p = new Power();
		System.out.println(p.power(2,7));
	}
}

- coolguy November 13, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

you can convert n to binary and use that. it is more clear that this code runs in O(logn)

function pow(x, n) {
	var bin = n.toString(2),
		logN = Math.floor(Math.log2(n)),
		pow = 1,
		exp = x;
	for (var i = 0; i <= logN; i++) {
		if (bin[logN - i] === '1') {
			pow *= exp;
		}
		exp *= exp;
	}
	return pow;
}

- nima November 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Python solution

def square(x): return x * x
def pow_exp(a,b):
    if b==0: 
        return 1
    if b%2==0: 
        return square(pow_exp(a,b/2))
    else:
        return (a * pow_exp(a,b-1) )

- Andre.S February 02, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Python solution

def square(x): return x * x
def pow_exp(a,b):
    if b==0: 
        return 1
    if b%2==0: 
        return square(pow_exp(a,b/2))
    else:
        return (a * pow_exp(a,b-1) )

- Andre.S February 02, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Main {
public static int power(int x, int n) {
if (n == 0) return 1;
else if (n == 1) return x;
else if (n % 2 == 1) {
return x * power(x, n-1);
} else {
int r = power(x, n/2);
return r * r;
}
}

public static void main(String[] args) {
int r;

r = Main.power(2,0);
System.out.println("power(2,0) = " + r);
r = Main.power(2,1);
System.out.println("power(2,1) = " + r);
r = Main.power(2,3);
System.out.println("power(2,3) = " + r);
r = Main.power(2,4);
System.out.println("power(2,4) = " + r);
}

/* (non-Java-doc)
* @see java.lang.Object#Object()
*/
public Main() {
super();
}

}

- stevelee1111 February 24, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

int power(int x, int n){
    if(n == 1)
    return x;
    else if(n == 0)
    return 1;
    else
    return x * power(x, n-1);

}

- Anonymous January 18, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

is this code produce log(n) time?

- Avinash January 18, 2015 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

int power(int x, int n){
    if(n == 1)
    return x;
    else if(n == 0)
    return 1;
    else
    return x * power(x, n-1);

}

- Xavien Pain January 18, 2015 | 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