## Amazon Interview Question for Software Engineer / Developers

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

long factorial(int n){
value = 1;
while(n > 0){
value = value * n;
n = n - 1;
}
return result;
}

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

sorry,

it should return value not result

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

long factorial(int n) {
long[] values = new long[n+1];
values[0] = 1;
for (int i = 1; i <= n; i++) {
values[i] = i * values[i - 1];
}
return values[n];
}

Comment hidden because of low score. Click to expand.
0

``````class FactorialHolder {
private static final int SIZE_OF_ARRAY = ??; // maximum of n satisfying n! < MAX_LONG
private static long[] values = new long[SIZE_OF_ARRAY];
static {
for(int i=0;i<SIZE_OF_ARRAY;i++) {
values[i] = 0;
}
values[1] = 1;
}
public static long factorial(int n) {
return values[n]>0 ? values[n] : values[n]=factorial(n-1)*n;
}
public static long factorialIterative(int n) {
int i;
for(i=n;i>=1;i--) {
if (values[i]!=0) break;
}
for(i=i+1;i<=n;i++) {
values[i] = values[i-1]*i;
}
return values[n];
}
}``````

Comment hidden because of low score. Click to expand.
0

if you can can use c++, template meta programming could be better, because factorial table is built at compile time(not runtime).

even in java, factorial value is fixed, you can build table and paste into source code.(and earn calculation time)

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

Problem:
-----------
Find Nth factorial

Example:
------------

N=5

Factorial F = 5*4*3*2*1 = 120

Algo/Solution:
-----------

Input : Factorial int N

A for loop which will decrement i from N to 0
and keep on multiplying with its value

Output : result

Code:
--------------

Private int NFactorial(Int N)
{

For (int i=N , i > 0,i-- )
{
int result *=i;
}

return Result;

}

HandRun:
--------------
N=5
i=5
5
5*4=20
20*3=60
60*2=120
120*1=120

Order:
-----------
O(N)

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

int fact(int n)
{
return (n<=1?1:fact(n-1)*n);

}

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

What will you do if there is an over flow ?

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.

### 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.