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

- chriscow January 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- pracky March 25, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- mani 4m sklm May 03, 2014 | Flag
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;
	}
}

- Coder January 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Have you tested power(0, -1)?

- chriscow January 25, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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)

- vinay August 03, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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)

- vinay August 03, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I was wondering if we can use log/antilog

- RShah January 26, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

decimal sayi = decimal.Parse(Console.ReadLine());
            decimal us = decimal.Parse(Console.ReadLine());
            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));
            Console.ReadKey();

it works

- Anonymous January 27, 2014 | Flag Reply
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.

- Alex February 05, 2014 | Flag Reply
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);
}

- Francis February 24, 2015 | Flag Reply
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;
}

- Anonymous July 11, 2016 | Flag Reply
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;
}

}

- yuviram91 July 31, 2016 | Flag Reply
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);
}

- confused_banda January 26, 2014 | Flag Reply
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);

}
}

- careerCupguy10 January 27, 2014 | Flag Reply
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);

}
}

}}

- careerCupguy10 January 27, 2014 | Flag Reply
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))
}
}

}

- nitin466 January 25, 2014 | Flag Reply
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.

- chriscow January 25, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- chriscow January 25, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- nitin466 January 27, 2014 | Flag


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