Amazon Interview Question


Country: India




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

If you Search google you will find this answer also ..
F(n) to reach n steps ..
then F(n) = F(n-1) + F(n -2) [ you can reach n only from n-1 and n -2 level ]
F(1) = 1;
F(2) = 2;
But permutation and combination answer is also correct !!

- MI December 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice solution.

- decoder December 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

public int CountWays(int N)
{
    if(N<0) return 0;
    else if(N==0) return 1;
    else
    {
        return CountWays(N-1) + CountWays(N-2);
    }
}

- cutie December 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Good answer.

A few notes:
- it is interesting to note that the answer is just the Nth Fibonacci number
- code can be improved using memoization/dynamic programming etc

- hakanserce January 01, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Total =sum of[ (x + y)!/(x!* y!)] where y ranges from 0 to (N - N%2)/N and x = N - 2*y

- Mr X December 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

y from 0 to (N - N%2)/2

- Mr X December 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think it should use dynamic programming. Start from N = 2 (which has two ways), and then increase N (= 3, 4, 5 ...)

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

That's very similar to Fibonacci Sequence

- Anonymous December 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

Your answer (as elaborated by Mr X) is right.

The fibonacci series answer is also right.

In fact what we have is an identity connecting the binomial coefficients and fibonacci numbers!

The interviewer seems to be a moron (or perhaps how well you convince him was part of the test).

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

int Q=0;
int FindNumberofWays(int N)
{
    if(N==1) return Q+1;
    if(N==2) return Q+1;
    if(N==0) return 0;
    return FindNumberOfWays(Q-1) +  FindNumberOfWays(Q-2);
}

- cutie December 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

At each step, one can take either one step or two steps. A simple Dynamic programming approach can be used.
Below is the working C code for n==6:

#include<stdio.h>
#include<conio.h>

int main(int n)
{
    int temp;
    int hash[7]={0};
    hash[0]=hash[1]=1;
    temp=func(6,hash);
    printf("%d",temp);
    getch();
    return 1;
}

int func(int n,int hash[])
{
    if(hash[n]!=0)
    return hash[n];
    int ways;
    ways=0;
    ways+=func(n-2,hash);
    ways+=func(n-1,hash);
    hash[n]=ways;
    return hash[n];
}

- k2 January 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is the first time I see DP combined with recursion...

- Sunny January 02, 2013 | 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