## Amazon Interview Question for Software Engineers

• 0

Country: India
Interview Type: In-Person

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

1) let's assume it's all integers so we can ignore negative exponents (e.g. 2^-1 = 1/2...) and there are no roots involved either
2) actually a negative exponent would be easy to do, just return a double and build the reciprocal 1/pow(base, -exp)

then it's about corner cases and overflow, try to be clean here

``````private static int power(int base, int exp) throws Exception{
if(exp < 0) throw new IllegalArgumentException("exponent must be >= 0"); // integer result, no reciprocal
if(exp == 0) return 1; // base^0 = 0 for all values of base
if(exp == 1) return base; // base^1 = base for all values of base
if(base == Integer.MIN_VALUE) throw new Exception("overflow"); // negative Min-Value has no positive counter part
if(base < 0) return (exp % 2 == 0) ? power(-base, exp) : -power(-base, exp); // negative base
if(base == 1) return 1; // no matter of the exponent, negative case already captured

int e = 1, r = 1;
while(true) {
if ((exp & e) > 0) r = savePosMul(r, base);
e <<= 1; // base overflows before the 1 is shifted out, except when base = 1, which is captured above
if(exp > e) base = savePosMul(base, base);
else break;
}
return r;
}

private static int savePosMul(int a, int b) throws  Exception {
assert (a >= 0 && b >= 0);
if(Integer.MAX_VALUE / a < b) throw new Exception("overflow");
return a * b;
}``````

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

This one seems quite straightforward:

``````from itertools import repeat
def getPower(a, b):
return reduce(lambda x, y: x * y, repeat(a, b))``````

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

C++ recursive approach just for fun

``````double powHelper(int x, int y)
{
return y == 1 ? x : x * powHelper(x, y - 1);
}

double pow(int x, int y)
{
// Corner cases and edge cases
if (y == 0 && x < 0) return -1;
if (x == 0 && y < 0) return -1u >> 1; // MAX_INT
if (y == 0) return 1;
if (x == 0) return 0;

// Use separate helper function to avoid
// duplicating corner/edge case tests
if (y > 0)
return powHelper(x, y);
else
return 1.0 / powHelper(x, -y);
}``````

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

``````public  int num(int number,int power){
int numb=1;
if(power==0){
return 1;
}
else{
for (int i = 0; i <power; i++) {
numb=numb*number;
}
}
return numb;``````

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

``````#include<bits/stdc++.h>
using namespace std;
int power(int x,int y)
{
if(y==0)
return 1;
int temp = power(x,y/2);
if(y%2==0)
return temp*temp;
else return x*temp*temp;
}
int main()
{
int a,b;
cin>>a>>b;
cout<<power(a,b)<<endl;
return 0;
}``````

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

Assumptions: exp is +ve number.

``````/**
* Returns 1 if exp is 0 or -ve number.
* @param base
* @param exp
* @return
*/
public static int power(int base, int exp) {
return ((exp <= 0 ) ? 1 : base * (power(base, exp - 1)));
}``````

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

private int CalPower(int a, int b)
{
if (b == 0)
{
return 1;
}

if (b > 1)
{
return a * CalPower(a, b-1);
}

return a;

}

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

``````public class Power {

public static int power(int n, int k) throws Exception
{
if(n < 0 || k < 0) throw new Exception("Cannot calculate the power");
if(k == 0) return 1;
if(k == 1) return n;

int result = 1;
for(int i = 1; i <= k; i++ )
{
result *= n;
}
return result;
}

public static int powerRecursive(int n, int k) throws Exception
{
if(n < 0 || k < 0) throw new Exception("Cannot calculate the power");
if(k == 0) return 1;
if(k == 1) return n;
return n * powerRecursive(n, k - 1);
}
public static void main(String[] args) throws Exception{
int n = 4, k = 3;
System.out.println(power(n,k));
System.out.println(powerRecursive(n,k));
}

}``````

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

int mathpower(int i, int j) {

return j==1 ?i: i*mathpower(i,j-1);

}

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

double power(double base, double index)
{
double pow=1;
while(index--)
{
pow=pow*base;
}
return pow;
}

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

Well, in Python this is quite simple.

def powerOf(int num1, int num2):
num1 = int(input())
num2 =int(input())
power = num1 **num2
print(power)

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

In python its far easier:
below code for any given value:

``````def power(x,y):
return (x**y)
x=int(input("Enter the value to find power:"))
y=int(input("Enter the value of power:"))
power(x,y)``````

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

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

return result;
}

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

Mohsin there are lot of bugs in you program; see my blog for details

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

public int num(int number,int power){
int numb=1;
if(power==0){
return 1;
}
else{
for (int i = 0; i <power; i++) {
numb=numb*number;
}
}
return numb;}}}

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

test

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

Mohsin, your code will not work, there are lot of bugs. See the blog for more details :) https://baquerrizvinotes.blogspot.in/2017/05/how-to-crack-amazoncom-technical.html#comments

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

Mohsin, your will not work, there are lot of bugs :)

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

Seems like the site is buggy, I am not able to reply to individual comments, so replying inline

Fernando, can you improve the tie complexity
Josh , have you considered overflow. Time complexity as also be improved
Chris, [let's assume it's all integers so we can ignore negative exponents (e.g. 2^-1 = 1/2...) and there are no roots involved either]. This is nota good assumption since you are taking integer as input

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.