Amazon Interview Question for Interns


Country: United States
Interview Type: Phone Interview




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

Is it to find the zeros in binary format?
If yes then:

count = 0
while (n>0) {
count = count + !(n&1)
n=n>>1 //Right shift by 1
}
return count

To find in decimal format the

count = 0
while (n>0) {
count = count + (n%10>0 ? 0 : 1)
n = n/10;
}
return count

- Punit Jain March 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

n>>1 Is it a left shift or right?
>> Indicates the bits are to be shifted to the right.

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

But in your code if there is a number with no zeros even then you would iterate over the entire string which is not efficient. Rather if you mask all your 1's and the if number masked number is 0 then clearly it contains no 0's so you don't need to check the entire string.

- shalinshah1993 June 30, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry, it was right shift, I have changed this. Shalini can you please elaborate through code. Thanks!

- Punit Jain June 30, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

OK. But for the binary format one, it will stuck in infinite loop if n is a negative number when use C/C++, due to ABS.

- Anonymous October 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

while(num!=0)
    {
        k=num%10;

        if(k==0)
        count++;


        num=num/10;

    }
    cout<<"\n"<<count;

- anonymous June 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It can be found by finding number of times 5 appears in the factors, since 5 *2 always gives one zero. So to proceed, try to find maximum value of n in 5^n in case where number is divisible.
Ex: 100 => 5^2 , therefore n = 2 so maximum trailing zeros are 2.

- Anonymous April 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this approach will give only trailing zeros count.

- truelies December 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int findZeroBit(int num)
{
int n=1,count=0;
while(n <= num)
{
if(!(n & num))
count++;
n<<=1;

}
return count;
}

- Aalok June 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int findZeroBit(int num)
{
int n=1,count=0;
while(n <= num)
{
if(!(n & num))
count++;
n<<=1;

}
return count;
}

- Aalok June 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I would it by counting the number of ones and subtracting from sizeof(integer).

Assume input = v

for (int c =0; v; v>>1)
c+= (v&1)

At end 'c' will contain number of ones in number

So number of zeros is:

NoOfZeros = sizeof(int) *8 - c

(8= number of bits in byte. I guess this is an assumption so the code may not be fully portable)

So final code

int NoOfZeros(int v)
{
for (int c = 0; v; v>>1)
c+= (v&1);

return sizeof(int)*8-c;
}

- asifnzaidi August 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int FindZeros(uInt val)
{
static uInt cnt = 0, loop = 0;
if(loop == 32)
{
return cnt;
}
loop++;
if(!(val & 1))
cnt += 1;
FindZeros(val >> 1);
}

- Nivas September 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int count_zero_bits(int n)
{
   int x = ~o;
   x=x^n;    // this will mask the bits that are set to 1, 
   int count=0;
  while(x!=0)
   {
      if(x&1==1)
       {
           count++;

       }
   x=x>>1;
  
   }

 return count;
}

- Anonymous October 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int count_zero_bits(int n)
{
   int x = ~o;
   x=x^n;    // this will mask the bits that are set to 1, 
   int count=0;
  while(x!=0)
   {
      if(x&1==1)
       {
           count++;

       }
   x=x>>1;
  
   }

 return count;
}

- Anonymous October 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

n = n&(n-1) : set last right most 1 to 0;
count number of 1's and subtract from (sizeof(int)*8)

int numberofzeros(int n)
{
int count =0;
while(n)
{ count++;
n= n&(n-1);
}
return ((sizeof(int)*8)-count);

}

- vathsala nagaraju September 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
using namespace std;
int findTrailingZeros(int n)
{
int count = 0,sum=0,res=0;

while(n>5)
{
res=n/5;
sum=sum+res;
n=res;
}
return sum;
}
int main()
{
int n,res;
cin>>n;
res=findTrailingZeros(n);
cout<<res;
return 0;
}

- vinay February 06, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
using namespace std;
int findTrailingZeros(int n)
{
int count = 0,sum=0,res=0;

while(n>5)
{
res=n/5;
sum=sum+res;
n=res;
}
return sum;
}
int main()
{
int n,res;
cin>>n;
res=findTrailingZeros(n);
cout<<res;
return 0;
}

- vinay February 06, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int findTrailingZeros(int n)
{
int count = 0,sum=0,res=0;

while(n>5)
{
res=n/5;
sum=sum+res;
n=res;
}
return sum;
}
int main()
{
int n,res;
cin>>n;
res=findTrailingZeros(n);
cout<<res;
return 0;
}

- vinay February 06, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int findTrailingZeros(int n)
{
int count = 0,sum=0,res=0;

while(n>5)
{
res=n/5;
sum=sum+res;
n=res;
}
return sum;
}
int main()
{
int n,res;
cin>>n;
res=findTrailingZeros(n);
cout<<res;
return 0;
}

- vinay February 06, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
using namespace std;
int findTrailingZeros(int n)
{

int count = 0,sum=0,res=0;

while(n>5)
{
res=n/5;
sum=sum+res;
n=res;
}
return sum;
}
int main()
{
int n,res;
cin>>n;
res=findTrailingZeros(n);
cout<<res;
return 0;

}

- vinay February 06, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
using namespace std;
int findTrailingZeros(int n)
{

int count = 0,sum=0,res=0;

while(n>5)
{
res=n/5;
sum=sum+res;
n=res;
}
return sum;
}
int main()
{
int n,res;
cin>>n;
res=findTrailingZeros(n);
cout<<res;
return 0;

}

- vinay February 06, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
using namespace std;
int findTrailingZeros(int n)
{
int count = 0,sum=0,r res=n/
sum=sum+res;
n=res;
}
return sum;
}
int main()
{
int n,res;
cin>>n;
res=findTrailingZeros(n);cout
<<res;
return 0;
}

- vinay February 06, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
using namespace std;
int findTrailingZeros(int n)
{
int count = 0,sum=0,r res=n/
sum=sum+res;
n=res;
}
return sum;
}
int main()
{
int n,res;
cin>>n;
res=findTrailingZeros(n);cout
<<res;
return 0;
}

- vinay February 06, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

For calculating the number of zeroes in the given integer we will have to check for n&1 == 0 every time after shifting the bit to the right by 1 and then, if found increment the count and hence, return the count when all the bits have been traversed.

Implementation:

#include<bits/stdc++.h>
using namespace std;
int countno(int r){
    //int r = ~n;
    int count = 0;
    while(r > 0){
        if((r&1) == 0)
            count++;
        r = r>>1;
    }
    return count;
}
int main(){

    int n;
    n = 2;
    cout<<countno(n)<<endl;
    return 0;

}

- swapnilkant11 July 20, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

It can be done as follows.
int num=1034000500;
int count =0;
while(num>0){
int modn=num%10;
num=num/10;
if(modn==0){
count++;
}

}

// count is the result.

- Fakaruddin April 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

for(int i = 0; i < sizeOf(int) * 8 ; i++)
{
x & x-1
count++;
}

Answer = sizeOf(int)*8 - count

Basically number of 1's minus the total possible binary digits.

- whizz.comp March 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

while (x) {
x &= x-1;
count ++;
}
return sizeof(int)*8 - count;

- to_google March 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

while (x) {
x &= x-1;
count ++;
}
return sizeof(int)*8 - count;

- to_google March 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

<<<
for(int i = 0; i < sizeOf(int) * 8 ; i++)
{
x & x-1
count++;
}
Answer = sizeOf(int)*8 - count
Basically number of 1's minus the total possible binary digits.
>>>

count will always be equal to sizeof(int)*8 so the code will always return zero

- Varun March 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

<<<
while (x) {
x &= x-1;
count ++;
}
return sizeof(int)*8 - count;
>>>

This does not returns the right number of zeroes.

- Varun March 29, 2012 | 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