## 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;
}
}

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

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;
}
}

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);
}
}

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));

}
}

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));
}

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));
}
}

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;
}

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) )

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) )

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();
}

}

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);

}

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

is this code produce log(n) time?

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);

}

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.

### 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.