Amazon Interview Question
Software Engineer / DevelopersCountry: United States
Good stuff.. but that made me believe that this question just have one good answer that needs to be remembered. And as such it is a bad question.
take var=1. then multiply var with 5 each time as long as number is less than given number.
Take remainder of number and again apply same above step.
int remainder (int a)
{
int mask = 1;
while ( (mask * 5) <= a )
{
mask *= 5;
}
return (a - mask);
}
void remainderOfFive (int a)
{
int rem = a;
while (rem >= 5)
{
rem = remainder (rem);
}
if (rem == 0)
cout << a << " is Multiple of 5" << endl;
else
cout << a << " Not Multiple of 5" << endl;
}
To make the solution cover negative value and more compact, modify it as below:
public boolean isMultiplesOfFive(int a) {
int mask = 5;
for (a= Math.abs(a); mask << 1 < a; mask <<= 1) {
}
for (; mask >= 5; mask >>= 1) {
if (a >= mask) {
a -= mask;
}
}
return a == 0;
}
This solution returns true if number is power of 5, not multiples of 5. For example it returns false for 10, but it should return true.
Here is simple code in java. Steps are as following
1. Divisibility can be checked by checking whether last digit is 0 or 5.
2. Since complexity should be o(logn) .
3. If number is odd we can add 5 in it.
5. After that we have to check whether last digit is 0 or not.
6. Now u we can multiply it with 0.1 and after that we can cast it again in int.
7.Then multiply with ten.
8.After all these processes we should get the old number back;
9.If we dont get it number is not divisible.
static boolean check(int x) {
if((x&1)==1)
x+=5;
float y=((int)(x*0.1))*10;
if(y==x)
return true;
else return false;
}
Another weird solution could be:
Convert the number into string and check if the last character in the string is either a 5 or 0.
If yes, then the number is multiple of 5. else not.
public class Multiple {
public static void main(String[] args) {
int number = 105;
String str = String.valueOf(number);
if (str.charAt(str.length() - 1) == '0' || str.charAt(str.length() - 1) == '5')
System.out.print("the number is a multiple of 5");
else
System.out.print("the number is not a multiple of 5");
}
}
yeah true :)..tried to do it with right shift..
public class MultipleOf5 {
public static void main(String[] args) {
int number = 100;
String str = Integer.toBinaryString(number);
while (number >= 5) {
if (str.charAt(str.length() - 1) == '1')
number = number - 5;
number = number >> 1;
str = Integer.toBinaryString(number);
System.out.println(number);
}
if (number == 0)
System.out.println("the number is divisble by 5");
else
System.out.println("the number is not divisble by 5");
}
}
how is this one?
bool isMultiplyOf5(int n)
{
bool is5 = false;
int i = 0;
int cur = 5;
for (; i < n; i ++) {
if (cur*5 > n) {
n -= cur;
if (n == 5 || n == 0) {
is5 = true;
break;
}
else if (n < 5 && n > 0) {
break;
}
else if(n > 5)
{
cur = 5;
continue;
}
}
else if(cur == n)
{
is5 = true;
break;
}
else
{
cur *= 5;
}
}
return is5;
}
int division(int tempdividend, int tempdivisor) {
int quotient = 1;
if (tempdivisor == tempdividend) {
remainder = 0;
return 1;
} else if (tempdividend < tempdivisor) {
remainder = tempdividend;
return 0;
}
do{
tempdivisor = tempdivisor << 1;
quotient = quotient << 1;
} while (tempdivisor <= tempdividend);
/* Call division recursively */
quotient = quotient + division(tempdividend - tempdivisor, divisor);
return quotient;
}
void main() {
printf ("\nEnter the Dividend: ");
scanf("%d", ÷nd);
printf("\nEnter the Divisor: ");
scanf("%d", &divisor);
printf("\n%d / %d: quotient = %d", dividend, divisor, division(dividend, divisor));
printf("\n%d / %d: remainder = %d", dividend, divisor, remainder);
getch();
}
Having a string as input, the problem becomes only checking wether the last char of the string is equals to 0 or 5.
In python, it would be:
last = sys.argv[1][len(sys.argv[1])-1]
print 'last digit: ' + last
if last == '5' or last == '0':
print 'Yes!'
else:
print 'No!'
If I am calculating it correctly, this is O(1), but I find it suspiciously easy.
Am I doing it right?
How about doing a binary search? Let's assume the input is n. If n is a multiple of 5, then it can be written that n = 5*x. We set low = 1, high = n, and try to find x using a binary search. Note that since w can't use / operator, we need to use right shift for computing mid = (low + high) >> 1.
bool divisabelBy5 (unsigned int n)
{
if (n < 5)
return false;
n -= 5;
if (n & 0x1)
return false;
n >>= 1;
int d = 0;
while (n) {
if (n & 0x1) d += 2;
if (n & 0x2) d += 4;
if (n & 0x4) d += 8;
if (n & 0x8) d += 6;
if (d >= 20) d -= 20;
else if (d >= 10) d -= 10;
n >>= 4;
}
return d == 0;
}
public boolean test(int n)
{
int temp=n*0.1;
temp=temp*10;
if(n-temp==0 || n-temp==5)
{
return true;
}
else
{
return false;
}
}
}
Algo:-
1. if number is greater than 5, check if odd. If odd, subtract 5.
2. if even, divide by 2. until it becomes an odd.
3. check in n=5.
The number will finally reduce to 5.
code:
public static boolean divisibleByFive(int n){
if(n==0)
return true;
if(n<0)
return divisibleByFive(n*(-1));
while(n>=5){
//if even divide by 2, until it becomes odd
while((n&1)==0)
n=n>>1;
//now check if equal to 5.
if(n==5)
return true;
else
n=n-5;
}
return false;
}
I feel i am missing something crucial here. if the number ends with a 0 or a 5, it is definitely divisible by 5. this is not a good question
Just found another way from geeks for geeks website.
A number n can be a mulpile of 5 in two cases. When last digit of n is 5 or 10. If last bit in binary equivalent of n is set (which can be the case when last digit is 5) then we multiply by 2 using n<<=1 to make sure that if the number is multpile of 5 then we have the last digit as 0. Once we do that, our work is to just check if the last digit is 0 or not, which we can do using float and integer comparison trick.
#include<stdio.h>
bool isMultipleof5(int n)
{
/* If n is a multiple of 5 then we make sure that last
digit of n is 0 */
if ( (n&1) == 1 )
n <<= 1;
float x = n;
x = ( (int)(x*0.1) )*10;
/* If last digit of n is 0 then n will be equal to (int)x */
if ( (int)x == n )
return true;
return false;
}
/* Driver program to test above function */
int main()
{
int i = 19;
if ( isMultipleof5(i) == true )
printf("%d is multiple of 5\n", i);
else
printf("%d is not a multiple of 5\n", i);
getchar();
return 0;
}
Just found another way from geeks for geeks website.
A number n can be a mulpile of 5 in two cases. When last digit of n is 5 or 10. If last bit in binary equivalent of n is set (which can be the case when last digit is 5) then we multiply by 2 using n<<=1 to make sure that if the number is multiple of 5 then we have the last digit as 0. Once we do that, our work is to just check if the last digit is 0 or not, which we can do using float and integer comparison trick.
#include<stdio.h>
bool isMultipleof5(int n)
{
/* If n is a multiple of 5 then we make sure that last
digit of n is 0 */
if ( (n&1) == 1 )
n <<= 1;
float x = n;
x = ( (int)(x*0.1) )*10;
/* If last digit of n is 0 then n will be equal to (int)x */
if ( (int)x == n )
return true;
return false;
}
/* Driver program to test above function */
int main()
{
int i = 19;
if ( isMultipleof5(i) == true )
printf("%d is multiple of 5\n", i);
else
printf("%d is not a multiple of 5\n", i);
getchar();
return 0;
}
think about how division is implemented on computer?
5 is 101;
20 is 10100;
if you left shift 101 for 2 bit, you will get 20, so ....
- Anonymous January 20, 2014