Amazon Interview Question for Software Engineer / Developers


Country: -
Interview Type: Phone Interview




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

F(1) = 1
F(2) = 2
F(3) = 4
F(n) = F(n-1) + F(n-2) + F(n-3)

- eugene.yarovoi October 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

f(n)=f(n-3)+1

f(0)=0
f(1)=1
f(2)=1

- Anonymous October 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

f(0) = 0
f(1) = 1
f(2) = 2
for n >= 3
f(n) = f(n-1) + f(n-2) + f(n-3)
// The last step is 1, 2, 3, so three disjoint set

- Are you sure your solution is correct? October 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Excuse me f(0) = 1

- sorry October 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

i think f(0) shud b = 0
since no of steps = 0,so there is no way to climb it

- kap October 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

i think f(0) shud b = 0
since no of steps = 0,so there is no way to climb it

- kap October 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can one of you give an example how f(n) = f(n-1) + f(n-2) + f(n-3) holds true say when n=4

- sb December 21, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

should not it be
F(1) = 1
F(2) = 2
F(3) = 4
F(n) = F(n-1) + 2*F(n-2) + 4*F(n-3)n ????
Or am i getting the question wrong

- code756 October 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

F(n-2) is covered by F(n-1) so, you don't need to multiply it with 2 again..same thing for F(n-3)

- Anonymous October 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think solution will be this
n<3
F(0) = 0;
F(1) = 1;
F(2) = 2;
n>=3
F(n) = F(n-1) + F(n-2) + F(n-3) + 1;

we will add 1 because F(n-1),F(n-2) & F(n-3) each will give number of ways they can be reached and 1 for reaching from them to F(n);

correct me if am wrong..

- Vikash Kesarwani October 31, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Please explain why is f(3)=4 and why do we need f(0)?

Won't f(3) be 3..
1) All 3 steps at a time
2) 2 at a time and then 1 last
3) each step at a time

Are we taking permutations of 2nd possibility? (1 step first and 2 later?)

Isn't this enough?
If n<3
F(1) = 1
F(2) = 2
F(3) = 3
and for n>=3
F(n) = F(n-2) + F(n-1)

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

for n>3
F(n) = F(n-1) + F(n-2) + F(n-3)

For n<=3
F(3) = 4
F(2) = 2
F(1) = 1

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

#include<iostream>
#include<stdio.h>

using namespace std;
static int total = 0;
int level = 40;

void steps(int totalstepstillnow)
{

    //printf("step = %d, totalstepstillnow = %d, \n",step,totalstepstillnow);
    if(totalstepstillnow == level)
    {
        //cout<<"Reached !\n";
        total++;
        return;
    }

    if(totalstepstillnow > level)
    {
        return;
    }

    steps(totalstepstillnow+1);
    steps(totalstepstillnow+2);
    steps(totalstepstillnow+3);
}

int main()
{
    steps(0);
    cout<<total<<endl;
}

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

Solution in the CCI book is as below.

class CountingSteps {
	public int countWaysRecursive(int n) {
		if (n < 0) {
			return 0;
		} else if (n == 0) {
			return 1;
		} else {
			return countWaysRecursive(n - 1) + countWaysRecursive(n - 2)
					+ countWaysRecursive(n - 3);
		}
	}
}

Here F(0) == 1. If there are 0 steps then there are no ways to climb it. Is that correct ? Why is the code look incorrect but is still correct ?

- CCI Solution March 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

There is exactly 1 way to climb 0 stairs. If anything is confusing here, it might be our informal notion, when using human language, of what constitutes a "way" to do something. We can make this notion precise: the number of ways to climb N steps with step sizes of 1, 2, and 3 is the number of distinct sequences consisting of 1, 2, and 3 whose elements sum up to exactly N. There is precisely 1 such sequence when N=0: the empty sequence.

That said, this recursive implementation has exponential complexity because it does not memorize answers to previously-solved subproblems. It can be augmented with dynamic programming to give a linear-time algorithm (though the simplest solution is still to solve it iteratively).

- eugene.yarovoi March 06, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

As i did not understand how can F(0) be 1 using above solution. Below is a revised one.

class CountingStepsAlternate {
	public int countWaysRecursive(int n) {
		if (n <= 0) {
			return 0;
		} else if (n == 1) {
			return 1;
		} else if (n == 2) {
			return 2;
		} else if (n == 3) {
			return 4;
		} else {
			return countWaysRecursive(n - 1) + countWaysRecursive(n - 2)
					+ countWaysRecursive(n - 3);
		}
	}
}

Output:

#1 Above) 1 1 2 4 7 13 24 44 81 149
#2 Alternate) 0 1 2 4 7 13 24 44 81 149

They differ at F(0) and i assume alternate solution is correct.

- CCI Alternate Solution March 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class CoutintStepsDP {
	public int countWaysRecursive(int n, int[] cache) {
		if (n <= 0) {
			return 0;
		} else if (n == 1) {
			return 1;
		} else if (n == 2) {
			return 2;
		} else if (n == 3) {
			return 4;
		} else if (cache[n] != -1) {
			return cache[n];
		} else {
			cache[n] = countWaysRecursive(n - 1, cache)
					+ countWaysRecursive(n - 2, cache)
					+ countWaysRecursive(n - 3, cache);
			return cache[n];
		}
	}
}

Input Steps: 0 1 2 3 4 5 6 7 8 9
Runs
Solution #1: 1 1 2 4 7 13 24 44 81 149
Solution #2: 0 1 2 4 7 13 24 44 81 149
Solution #3: 0 1 2 4 7 13 24 44 81 149

- CCI Solution - Using cache to improve speed March 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Problem1 {
	public static void main(String[] args) {
		CountingSteps c = new CountingSteps();
		for (int i = 0; i < 10; i++) {
			System.out.printf("%5d", c.countWaysRecursive(i));
		}
		System.out.println();
		CountingStepsAlternate cA = new CountingStepsAlternate();
		for (int i = 0; i < 10; i++) {
			System.out.printf("%5d", cA.countWaysRecursive(i));
		}

		System.out.println();
		CoutintStepsDP cDP = new CoutintStepsDP();
		int cache[] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
				-1 };
		for (int i = 0; i < 10; i++) {
			System.out.printf("%5d", cDP.countWaysRecursive(i, cache));
		}
	}
}

/**
 * # of Steps: 0 1 2 3 4 5 &nbsp;6 &nbsp;&nbsp;7 &nbsp;&nbsp;&nbsp;8
 * &nbsp;&nbsp;&nbsp;9 <br/>
 * # of Ways : 1 1 2 4 7 13 24 44 81 149 &nbsp;81
 * 
 */
class CountingSteps {
	public int countWaysRecursive(int n) {
		if (n < 0) {
			return 0;
		} else if (n == 0) {
			return 1;
		} else {
			return countWaysRecursive(n - 1) + countWaysRecursive(n - 2)
					+ countWaysRecursive(n - 3);
		}
	}
}

class CountingStepsAlternate {
	public int countWaysRecursive(int n) {
		if (n <= 0) {
			return 0;
		} else if (n == 1) {
			return 1;
		} else if (n == 2) {
			return 2;
		} else if (n == 3) {
			return 4;
		} else {
			return countWaysRecursive(n - 1) + countWaysRecursive(n - 2)
					+ countWaysRecursive(n - 3);
		}
	}
}

class CoutintStepsDP {
	public int countWaysRecursive(int n, int[] cache) {
		if (n <= 0) {
			return 0;
		} else if (n == 1) {
			return 1;
		} else if (n == 2) {
			return 2;
		} else if (n == 3) {
			return 4;
		} else if (cache[n] != -1) {
			return cache[n];
		} else {
			cache[n] = countWaysRecursive(n - 1, cache)
					+ countWaysRecursive(n - 2, cache)
					+ countWaysRecursive(n - 3, cache);
			return cache[n];
		}
	}
}

- Final Solution March 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

cool

what about N = 60
?

- drug June 15, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bottom up approach

int waysnsteps(int n) {
    vector<int> cache(n+1, 0);
    cache[0]=0;
    cache[1]=1;
    cache[2]=2;
    cache[3]=4;
    int i=4;
    while (i<=n) {
	    cache[i]=cache[i-1]+cache[i-2]+cache[i-3];
	    i++;
    }
    return cache[n];
}

steps 0 no of ways 0
steps 1 no of ways 1
steps 2 no of ways 2
steps 3 no of ways 4
steps 4 no of ways 7
steps 5 no of ways 13
steps 6 no of ways 24
steps 7 no of ways 44
steps 8 no of ways 81
steps 9 no of ways 149
steps 10 no of ways 274

- sn October 14, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int waysnsteps(int n) {
    vector<int> cache(n+1, 0);
    cache[0]=0;
    cache[1]=1;
    cache[2]=2;
    cache[3]=4;
    int i=4;
    while (i<=n) {
	    cache[i]=cache[i-1]+cache[i-2]+cache[i-3];
	    i++;
    }
    return cache[n];

}

- sn October 14, 2019 | Flag Reply


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