Amazon Interview Question


Country: India




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

int fibnums = int[] //fill with fib numbers less than 64;
int[][] cvals = int[64][64] //fill N(c,r) values using N(c,r) = N(c-1,r-1)+N(c-1,r)

fibonacci(int a, int b){
    int r1 = findrank(a-1);
    int r2 = findrank(b);
    return r2-r1;
}

findrank (int a){
    cnt = 0;
    cintex = 64;
    int onesfound = 0;
    while(cnintex<0){
        int cbit = a>>(--cintex);
        if(cbit == 1){
            onesfound++;
        }
        if(onesfound == 0){
            continue;
        }
        else{
            if(onesfound == 1 && cbit == 1){
                cnt = getfibnumbercount(cintex+1,0);
            }
            if(cbit == 0){
                cnt -= getfibnumbercount(cintex,onesfound+1);
            }
        }
    }
    return cnt;
}

getfibnumbercount(numdigits, foundones){
    int cnt = 0;
    for(int v : fibnums){
        if(v >= foundones){
            cnt += cvals[numdigits][v-foundones];
        }
    }
    return cnt;
}

- anonymous July 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

not understanding this approach...can you please explain?

- rithesh July 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I'm not sure whether your code works. But, you got the point!

- LoocalVinci September 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

i think a permutation ways:
for example:
5 20
5: 0101
20 1100
in 4 bit, only fibonacci is 1, 2, 3,
so : the 1 between the range only in the forth position 1000 : so only one
and so on you could use permutation to solve it;
maybe , some errors; just ideas;

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

find rank of a and rank of b; find difference in ranks.
where rank for 10101 can be found as [c(5,1)+c(5,2)+C(5,3)+C(5,5)]-[C(3,0)+C(3,1)+C(3,3)]-[C(1,1)],
basically what we looking is total how many can be found with 5 digits subtract numbers which are greater than the current.

- annonymous July 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

what about the first number of the range?? how you will identify the minimum of the range and make sure you dont go beyond that.

- 14.ashish July 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

u can use first number-1;
we have to find rank(b)-rank(a-1) where b>a;
To find rank of a unmber, lets say it has n bits(last non-zero bit is n-1th), then get count of fib numbers having n or less bits, then subtract for each 0 bit assume it to be one and compute how many fib numbers can be found fixing the preix part and changinh the least bits.

- annonymous July 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

either i'm not getting your point or you have not understood the problem completely... if you don't mind can you write a small code or algo to explain your logic in a better way.

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

I think in this question we have to optimize a way to count no. of set bits....and store numbers into array then check for fibbo...using f(n)=f(n-1)+f(n-2) on array inputs

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

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<cstring>

using namespace std;

int fab[100];
int dp[70][70][2];

long long right;
void check()
{
int a = 0;
int b = 1;
int c = 0;
fab[0] = 1;
fab[1] = 1;
while ( c <= 64 ) {
c = a + b;
fab[c] = 1;
a = b;
b = c;
}
}

long long solve( int p , int c , int eq )
{
if ( p == -1 ) {
if( fab[c] == 1 )
return 1;
else
return 0;
}

if( dp[p][c][eq] != -1 )
return dp[p][c][eq];

extern long long right;
long long ans = 0;
if( eq ) {
if( ( 1<<p ) & right ) {
ans = ans + solve( p - 1 , c + 1 , 1 );
ans = ans + solve( p - 1 , c , 0 );
} else {
ans = ans + solve( p - 1 , c , 1 );
}
} else {
ans = ans + solve( p - 1 , c + 1 , 0 );
ans = ans + solve( p - 1 , c , 0 );
}

dp[p][c][eq] = ans;


return ans;
}

int main()
{
int n;

int t;
cin >> t;
memset( fab , 0 , sizeof( fab ) );
memset( dp , -1 , sizeof( dp ) );
check();

long long a , b , ans;
extern long long right;
while( t )
{
t--;
memset( dp , -1 , sizeof( dp ) );
fflush( stdin );
cin >> a >> b;
right = b;
ans = solve( 30, 0 , 1 );
if( a > 0 )
right = a - 1;
else
right = 0;

memset( dp , -1 , sizeof( dp ) );
right = a - 1;
ans = ans - solve( 30 , 0 , 1 );

cout << ans << "\n";
}

cin >> n;
return 0;
}

- iit2009047 July 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

void main()
{
int no;
printf("Enter the no.");
scanf("%d",&no);

for(int i=no; i<no+64;i++)
{
int r = fibonnaci(i);
if(r==1)
{
do{
i=i%2;
if(i==1)
p++;
i=i/2;
}while(i>0);

}else
printf("no. is not in fibonnaci series");

}

int fibonnaci(int w)
{ int a=0,b=1,c;
for(int i=0; i<w;i++)
{
if(c==w)
return 1;
else
{
c=a+b;
a=b;
b=c;
}
}
}

- P.B July 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

i think the best way to achieve it is.
1-First create a fib array which contains all fibonacci numbers between 0 and 64, there will be at most 11.
2-then count no. of set bits in A and B in O(1) time.
3-after that do a binary search on fib array to find no. of fibonacci numbers in b/w with O(1).

- nirmal.singh.cer08@itbhu.ac.in December 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.


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