## PayPal Interview Question for Software Engineer / Developers

Country: United States
Interview Type: Written Test

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

I assume b is integer.

Java code:

public double power(double a, int b) {
// Base case with 0 exponent
if (b == 0) return 1;

// Negative exponent
if (b < 0) {
assert a != 0; // Negative exponent for base 0 is not allowed
return 1. / power(a, -b);
}

// General case of positive exponent
double half = power(a, b / 2);
return half * half * ( (b % 2 == 1) ? a : 1 );
}

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

public class Power {

public static int calPower(int a, int b){

if(a==0)
return 0;

if(b==0)
return 1;

int result =calPower(a,b/2);

if(b%2==0)
return result * result;
else
return a * result * result;

}

public static void main(String[] args) {

System.out.println("result "+calPower(10,3));
}
}

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

if a==0 and b==0 then u have to return NaN

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

Works for both positive and negative exponent.
Memory efficient implementation. Complexity : O(log n)

public class Practice63 {
public static void main(String[] args){
int a = 2;
int b = 3;
System.out.println(power(a,b));
}

public static double power(int a, int b){
double temp = 1;
boolean isNeg = false;
if(b<0){
b = -b;
isNeg = true;
}
while(b>0){
if((b&1) == 1){
temp = temp * a;
}
a = a*a;
b = b>>1;
}

if(isNeg)
return 1/temp;
else
return temp;
}
}

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

Have you tested power(0, -1)?

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

to satisfy power(0,-1), change the last condition as

if(isNeg && temp > 0)
return 1/temp

This is to avoid 1/0 condition, in case for a = 0 and b = -1(negtive value)

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

To satisfy power(0,-1), change the last condition as below

if(isNeg && temp > 0)
return 1/temp;
This is to avoid 1/0 condition, in case for a = 0 and b = -1(negtive value)

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

I was wondering if we can use log/antilog

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

decimal sonuc = 1;
for (int i = 0; i < Math.Abs(us); i++) sonuc *= sayi;
if (sayi == 0 && us < 0)
Console.WriteLine("Can't divide by 0");
else
Console.WriteLine(us > 1 ? sonuc : Convert.ToDecimal(1 / sonuc));

it works

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

That is easy if b is integer: just binary power.
But more interesting problem is when b is not integer. I was asked it a while ago. Pretty nice problem.

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

This is enough.. x is base and y is the power

private static void calculatePower(int x, int y) {
int res = x;
for(int i=1; i<y; i++) {
res = res*x;
}
System.out.println(res);
}

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

long int power(int a,int b) {

long power = 1;
while(b>1) {
power = power * a;
b--;
}
return power;
}

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

package com.math.util;

public class ExponentClass {

public static void main(String[] args) {
ExponentClass exponentClass = new ExponentClass();
System.out.println(" Power of base " + exponentClass.power(5, 4));
}

public int power(int base, int exponent) {
int result = 1;
for (int i = 0; i < exponent; i++) {
result = result * base;
}

return result;
}

}

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

a^b meaning a to power of b?

unsigned int power(int a, int b)
{
if( a==  0 || a == 1 || b == 1 )
return a;
if( b & 0x1  == 0 )
{
unsigned int x = power(a,b>>1);
return x*x;
}
else
return a * power(a,b-1);
}

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

public static double getPower(double a, double b){

if(b==0) return 1;
//if b is negative
if(b<0){
return 1/a*(getPower(a,b+1));
}

return a*getPower(a,b-1);

}
}

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

This works with +ve as well as -ve Numbers{{
public static double getPower(double a, double b){

if(b==0) return 1;
//if b is negative
if(b<0){
return 1/a*(getPower(a,b+1));
}

return a*getPower(a,b-1);

}
}

}}

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

Assuming b is integer and for b negative we can make some minor change in code to get the result.

public class Indices {

public static int prod(int a, int b) {

if(a==0)
return 0;
else if (b==0)
return 1;
else
return a*prod(a, b-1);

}
public static void main(String[] args){
System.out.println(prod(a, b))
}
}

}

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

Your solution is effectively the same as

int result = 1;
for (int i = 1; i<b; i++) result *= a;

and you pay the extra cost of recursive function call and memory space for recursion.

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

The output for 0^0 should be 1, but your prog returns 0.

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

@chriscow: you are right , thanks for suggestion abt recursive function and for output 0^0, the solution can be rectified by order of changing the condition statement :
Also i think result should be initialized with value of a rather than 1(which result in giving output a^(b-1))
statement :

public class Indices {

public static int prod(int a, int b) {
if(b==0)
return 1;
else if (a==0)
return 0;

int result = a;
for (int i = 1; i<b; i++) result *= a;
return result ;
}
public static void main(String[] args){
System.out.println(prod(a,b));
}
}

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.